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
總結
- 上一篇: JavaScript中弧度和角度的转换
- 下一篇: [UML]UML 教程