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函数的更多相关文章

  1. index+match函数在压实度中对盒号盒质量随机不重复的最佳使用

    首先按照升序排列好盒号和盒质量,使其一一对应, 盒号    盒重量    随机值rand()     随机值大小排列rank 1         2001       0.01             ...

  2. Excel——MATCH函数

    使用 MATCH 函数在范围单元格中搜索特定的项,然后返回该项在此区域中的相对位置. 1.参数说明: MATCH(lookup_value, lookup_array, [match_type]) l ...

  3. EXCEL 2010学习笔记 —— VLOOKUP函数 嵌套 MATCH 函数

    match index vlookup 等函数都是查找引用类函数,需要查找的时候关键变量只有两个,区域+位置,区域的选择注意是否需要锁定,位置的确定可以通过输入特定的行号和列号. match() ma ...

  4. js match函数注意

    match函数 String.prototype.match 参数 regexp 返回 返回包含所有匹配的数组,如果匹配失败返回Null. 数组第一项是整段字符串的匹配,第二项至以后都是捕获匹配. 注 ...

  5. 使用Index()+Match()函数实现更为灵活的VLookUp()

    上一篇 http://www.cnblogs.com/-SANG/p/8407017.html 文章中已经介绍了vlookup的用法. 今天要使用index+match实现更为灵活的vlookup 先 ...

  6. awk之match函数

    功能:match函数是用于个性化定制搜索模式. 例子: 文件内容: this is wang ,not wan that is chen, not che this is chen ,and wang ...

  7. Excel中MATCH函数的正确使用

    Excel中MATCH函数是一个很强大的辅助函数, MATCH函数语法为:MATCH(lookup_value,lookuparray,match-type) lookup_value:表示查询的指定 ...

  8. JS中exec函数与match函数的区别与联系

    总结: 正则规则的声明,两种方法: exec是RegExp类的匹配方法 match是字符串类的匹配方法 var reg = /aaa/g; var reg = new RegExp("aaa ...

  9. excel 两列值匹配取另外一列值 INDEX MATCH 函数

    =INDEX($D:$D,MATCH(K68,$C:$C,0))

随机推荐

  1. [ARC100]E:Or Plus Max(FZT)

    https://arc100.contest.atcoder.jp/tasks/arc100_c 一个很自然的想法是,对于每个K求出i or j=k的所有a[i]+a[j]的最大值ans[k],答案就 ...

  2. [BZOJ 3233] 找硬币

    Link: BZOJ 3233 传送门 Solution: 在本蒟蒻看来算是一道比较神的$dp$了 一开始转移方程都没看出来…… 首先,如果确定了最大面值,是能推出其他面值的所有可能值的 从而发现最大 ...

  3. access日志配置

    链接地址:  https://wenku.baidu.com/view/3e20fac758f5f61fb73666cf.html org.apache.catalina.valves.AccessL ...

  4. 使用FluentValidation来进行数据有效性验证

    之前我介绍过了使用系统自带的Data Annotations来进行数据有效性验证,今天在CodePlex上逛的时候,发现了一个非常简洁好用的库:FluentValidation 由于非常简洁,就直接拿 ...

  5. Asp.net Core学习文章

    杜现鹏的Asp.net Core文章 EF Core 官方教程 https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/new- ...

  6. java以流的形式输出文件

    原文:http://blog.csdn.net/liutt55/article/details/78126614 public void downProcessFile(HttpServletRequ ...

  7. 【spring data jpa】使用jpa的@Query,自己写的语句,报错:org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'status' cannot be found on null

    报错: org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'status' ...

  8. scrapy-splash抓取动态数据例子十五

    一.介绍 本例子用scrapy-splash爬取电视之家(http://www.tvhome.com/news/)网站的资讯信息,输入给定关键字抓取微信资讯信息. 给定关键字:数字:融合:电视 抓取信 ...

  9. SSH 错误解决案例1:Read from socket failed: Connection reset by peer

    今天早上天天连接的开发机突然报出连接错误. 这个错误是SSH最常见错误,造成的原因也是千奇百怪(具体可goole),下面描述我的server的问题: 客户端报错 [root@server]# ssh ...

  10. C#趣味程序---求两个数的最大公约数和最小公倍数

    using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Cons ...