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))
随机推荐
- 【kd-tree】bzoj4066 简单题
同p1176. #include<cstdio> #include<cmath> #include<algorithm> using namespace std; ...
- 动态NAT地址转换
1.配置路由器的端口ip地址(注意外网和内网ip地址的设置) Router(config)#inter f0/0 Router(config-if)#ip add 192.168.1.1 255.25 ...
- NHibernate官方文档中文版--只读实体类型(Read-only entities)
重点 NHIbernate处理只读对象的方式可能和你在别处看到的不同.不正确的使用方式可能造成不可预料的结果. 当一个实体是只读的时候: NHIbernate不会对实体的简单属性和单向关联数据检查数据 ...
- SQLSERVER LATCH WINDBG
https://mssqlwiki.com/2012/09/07/latch-timeout-and-sql-server-latch/
- 【mybatis】 mybatis在mysql 更新update 操作 更新时间字段按照年月日时分秒格式 更新为当前时间
示例代码如下: update goods_msg SET create_date = DATE_FORMAT(NOW(),'%Y-%m-%d %H:%m:%s') WHERE uid = '6183b ...
- isNaN使用的注意事项
NaN是JavaScript的特殊值,表示 Not a Number 用法: isNaN(numValue); 如果值是 NaN, 那么 isNaN 函数返回 true ,否则返回 false . 注 ...
- shell用法 (cat << EOF)
下面的语句会创建不存在的secure.config,如果存在直接追加,然后把多行内容: [database] password = gerrit 写入文件secure.config ca ...
- supervisord的环境变量的设置
在python中引用jar包,使用java的功能,需要使用三方包:pyjnius from jnius import autoclass 需要java环境,因此需要设置JAVA_HOME环境变量. 但 ...
- 安装完office2016 64位后,在安装visio时,报错,无法安装,
安装环境要求: 系统要求:win8,win10等: office要求:sw(批量版)不能和cn(零售版).365版混装.-------重点注意事项 一定要注意批量版和零售版的区别,各版本之间绝对不允许 ...
- mongodb权限管理(转)
Mongodb 预定义角色 Mongodb 中预定义了一些角色,把这些角色赋予给适当的用户上,用户就只能进行角色范围内的操作. 数据库用户角色 (所有数据库都有) read 用户可以读取当前数据库的数 ...