zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    在Power BI报表和仪表板中显示刷新日期时间
    微软Power BI 每月功能更新系列——12月Power BI 新功能学习
    在Microsoft Power BI中创建地图的10种方法
    您应该将报表从Excel转换为Power BI的8个原因
    OBS录制全屏游戏的方法(超好录屏)
    关于Adobe Premiere Pro视音频不同步的解决方法
    Npcap:Nmap项目里一个为Windows而生的嗅探库 Npcap: Nmap Project's packet sniffing library for Windows
    关于被malloc分配内存的指针
    VS2017 高级使用方法
    Npcap环境配置(Winpcap后继者) pcap的一种
  • 原文地址:https://www.cnblogs.com/black-mamba/p/8875747.html
Copyright © 2011-2022 走看看