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

  • 相关阅读:
    Django如何把数据库里的html格式输出到前端
    如何修改Django中的日期和时间格式 DateTimeField
    python2.7无法安装python-ldap、django-auth-ldap
    windows10下Python如何设置环境变量
    微信小程序在开发者工具页面显示空白且控制台看不到报错信息
    CentOS7 升级 openssh 到 openssh-8.0p1版本
    CentOS系统升级OpenSSH版本
    SSL相关漏洞解决方法
    CentOS 7.4安装 MySQL数据库
    Python3 基础知识
  • 原文地址:https://www.cnblogs.com/black-mamba/p/8875747.html
Copyright © 2011-2022 走看看