match函数
match(s, r [, a])
Return the position in s where the regular expression r occurs, or 0 if r is not present, and set the values of RSTART and RLENGTH. Note that the argument order is the same as for the ~ operator: str ~ re. If array a is provided, a is cleared and then elements 1 through n are filled with the portions of s that match the corre‐sponding parenthesized subexpression in r. The 0'th element of a contains the portion of s matched by the entire regular expression r. Subscripts a[n, "start"], and a[n, "length"] provide the starting index in the string and length respectively, of each matching substring.
1. Return the position in s where the regular expression r occurs, or 0 if r is not present
如果匹配到,match函数返回第一个字符的position;如果没有匹配到,match函数返回0。
2. set the values of RSTART and RLENGTH
RSTART:若匹配到,则该值为 match函数 所匹配到的字符串的 开始位置;否则 该值为0。
RLENGHT:若匹配到,则该值为 match函数 所匹配到的字符串的 总长度;否则 该值为-1。
tmp.txt的内容
frank@ubuntu:~/test/tmp$ cat tmp.txt

1. RSTART 和 RLENGTH的含义
frank@ubuntu:~/test/tmp$ awk '{if(match($0,/^\[.+\]$/)){print $0, RSTART, RLENGTH}}' tmp.txt
[Frank] 1 7
[Mike] 1 6
这句命令的作用是:匹配以"["开头和以"]" 结尾的内容。
RSTART:若匹配到,则该值为 match函数 所匹配到的字符串的 开始位置;否则 该值为0。
RLENGHT:若匹配到,则该值为 match函数 所匹配到的字符串的 总长度;否则 该值为-1。
2. match函数的返回值
frank@ubuntu:~/test/tmp$ awk -vr=0 '{r=match($0,/^\[.+\]$/); print $0, RSTART, RLENGTH, r}' tmp.txt
[Frank] 1 7 1
id=123 0 -1 0
age=18 0 -1 0
0 -1 0
[Mike] 1 6 1
id=456 0 -1 0
age=18 0 -1 0
Return the position in s where the regular expression r occurs, or 0 if r is not present
如果匹配到,match函数返回第一个字符的position;如果没有匹配到,match函数返回0
3. ~ operator
正则匹配运算符
frank@ubuntu:~/test/tmp$ awk -vr=0 '{if($0~/^\[.+\]$/){print $0}}' tmp.txt
[Frank]
[Mike]
4. match 有第三个参数的情况
frank@ubuntu:~/test/tmp$ awk -vr=0 '{r=match($0,/^\[.+\]$/,a);{print $0, RSTART, RLENGTH, r, a[0]}}' tmp.txt
[Frank] 1 7 1 [Frank]
id=123 0 -1 0
age=18 0 -1 0
0 -1 0
[Mike] 1 6 1 [Mike]
id=456 0 -1 0
age=18 0 -1 0
match函数的更多相关文章
- index+match函数在压实度中对盒号盒质量随机不重复的最佳使用
首先按照升序排列好盒号和盒质量,使其一一对应, 盒号 盒重量 随机值rand() 随机值大小排列rank 1 2001 0.01 ...
- Excel——MATCH函数
使用 MATCH 函数在范围单元格中搜索特定的项,然后返回该项在此区域中的相对位置. 1.参数说明: MATCH(lookup_value, lookup_array, [match_type]) l ...
- EXCEL 2010学习笔记 —— VLOOKUP函数 嵌套 MATCH 函数
match index vlookup 等函数都是查找引用类函数,需要查找的时候关键变量只有两个,区域+位置,区域的选择注意是否需要锁定,位置的确定可以通过输入特定的行号和列号. match() ma ...
- js match函数注意
match函数 String.prototype.match 参数 regexp 返回 返回包含所有匹配的数组,如果匹配失败返回Null. 数组第一项是整段字符串的匹配,第二项至以后都是捕获匹配. 注 ...
- 使用Index()+Match()函数实现更为灵活的VLookUp()
上一篇 http://www.cnblogs.com/-SANG/p/8407017.html 文章中已经介绍了vlookup的用法. 今天要使用index+match实现更为灵活的vlookup 先 ...
- awk之match函数
功能:match函数是用于个性化定制搜索模式. 例子: 文件内容: this is wang ,not wan that is chen, not che this is chen ,and wang ...
- Excel中MATCH函数的正确使用
Excel中MATCH函数是一个很强大的辅助函数, MATCH函数语法为:MATCH(lookup_value,lookuparray,match-type) lookup_value:表示查询的指定 ...
- JS中exec函数与match函数的区别与联系
总结: 正则规则的声明,两种方法: exec是RegExp类的匹配方法 match是字符串类的匹配方法 var reg = /aaa/g; var reg = new RegExp("aaa ...
- excel 两列值匹配取另外一列值 INDEX MATCH 函数
=INDEX($D:$D,MATCH(K68,$C:$C,0))
随机推荐
- 【最短路】【spfa】CODEVS 2645 Spore
spfa最短路+判负权回路(是否某个点入队超过n次). #include<cstdio> #include<queue> #include<cstring> usi ...
- [CF509F]Progress Monitoring
题目大意: 给定一个树的DFS序$b_1,b_2,\ldots,b_n$($b$为$1\sim n$的一个排列且$b_1=1$).同一个结点的子结点按照结点编号从小到大遍历.问有多少种可能的树的形态? ...
- Vue 单页应用:记事本
若文章中存在内容无法加载的情况,请移步作者其他博客. 简书 CSDN 最近在看 Vue 的时候,别人给我安利了一个国外的小案例,通过 Vue 和 Vuex 来实现一个记事本. 仔细剖析下,发现“麻雀虽 ...
- python3用pyqt5开发简易浏览器
http://python.jobbole.com/82715/ 在这篇教程中,我们会用 Python 的 PyQt 框架编写一个简单的 web 浏览器.关于 PyQt ,你可能已经有所耳闻了,它是 ...
- Snapdragon profiler
这个debugger似乎看不了constant buffer 看不了memory but有个很神奇的功能 改shader直接在手机上显示结果 注意 需要unity build的时候勾 Script D ...
- 蒙皮 skin
http://help.autodesk.com/view/MAYAUL/2016/CHS/?guid=GUID-EFE68C08-9ADA-4355-8203-5D1D109DCC82 skin:顶 ...
- android应用开发-从设计到实现 2-8 组件与经常使用模式
组件与经常使用模式 前面已经比較全面的介绍了Material Design相关的设计哲学.设计原理和方法论. 这一章開始,我们将看看这些原理是怎样在安卓系统其中得到实践的. 一个应用并非全然从什么都没 ...
- 使用Faric+Git进行分布式代码管理
Fabric是一个Python库,可以通过SSH在多个host上批量执行任务. 可以通过编写任务脚本,然后通过Fabric在本地就可以使用SSH在大量远程服务器上自动运行. 这些功能非常适合应用的自动 ...
- grep怎样匹配tab键
grep怎样匹配tab键 学习了:https://blog.csdn.net/qixinkui/article/details/2746433 1 grep -P '/t'; 2 awk '//t/' ...
- JSON,字符串,MAP转换
package com.tree.autotest.testcase.IAuditBillDetailService; import com.alibaba.fastjson.JSON;import ...