正则表达式

正则表达式或许是某些程序员一辈子的苦手,特别是不同语言间的正则表达式还有些许细微但绝非不重要的差别,所以是有必要单独立一个小节来说明一下awk正则表达式的。
首先看下面的元字符表:

元字符说明
+匹配1个或多个前导字符的内容,如:/so+/ 可以匹配“soom”,“so”,但是不能匹配“is”,“saab”
?匹配0个或多个前导字符的内容,如:/so?/ 既可以匹配“soom”,“so”,也可以匹配“is”,“saab”,但是不能匹配“able”
|分隔2边的模式都可以用来匹配,如:/her/she/ 既可以匹配“her”,也可以匹配“she”
()用来将包围字符串作为一个整体,如:/do(ing)?/,可以匹配“do”和“doing”
[string]指定可以出现其中的任意字符,如:/th[b-z]/,匹配“the”,而不匹配“that”
[^string]指定不匹配的字符范围,如:/th[^a, \s]/与/th[b-z]/效果类似
^匹配行首字符,如:/^I/,匹配字符串行首字母是“I”的
$匹配行尾字符,如:/y$/,匹配所有以“y”结尾的字符串
.表示1个除换行符外的任意字符,如:/h......d/,表示匹配h开头,d结尾,中间有6个字符的子串
*表示0或多个换行外的任意字符,如:/h*d/表示匹配h开头,遇到首个d结尾的子串,/h.*d/是贪婪模式,表示匹配h开头,最后这个d结尾的子串
/转义字符

具体使用可以参考下面例程:

chapter_3_7-1.awk

BEGIN{
}
{
    content = "I was in love with her, so deeply in love that I didn’t care if she was sick. I didn’t care that we wouldn’t have long together. None of those things mattered to me. All I cared about was doing something that my heart had told me was the right thing to do. In my mind it was the first time God had ever spoken directly to me, and I knew with certainty that I wasn’t going to disobey. I know that some of you may wonder if I was doing it out of pity. Some of the more cynical may even wonder if I did it because she’d be gone soon anyway and I wasn’t committing much. The answer to both questions is no. I would have married Jamie Sullivan no matter what happened in the future. I would have married Jamie Sullivan if the miracle I was praying for had suddenly come true. I knew it at the moment I asked her, and I still know it today";

    # '+'匹配1个或多个
    idx = match(content, /so+/, result);
    if ( idx > 0 ) {
            print("match 1:");
            for( key in result ) {
                    print(result[key]);
            }
            print("");
    }

    # '?'匹配0个或多个
    idx = match(content, /so?/, result);
    if ( idx > 0 ) {
        print("match 2:");
        for( key in result ) {
            print(result[key]);
        }
        print("");
    }

    # '|' 分隔的2边都可以进行匹配
    idx = match(content, /her|she/, result);
    if ( idx > 0 ) {
        print("match 3:");
        for( key in result ) {
            print(result[key]);
        }
        print("");
    }

    # '()' 将包围字符串作为一个整体
    idx = match(content, /do(ing)?/, result);
    if ( idx > 0 ) {
        print("match 4:");
        for( key in result ) {
            print(result[key]);
        }
        print("");
    }  

    # '[]' 指定匹配字符范围
    idx = match(content, /th[b-z]/, result);
    if ( idx > 0 ) {
        print("match 5:");
        for( key in result ) {
            print(result[key]);
        }
        print("");
    }

    # '[^]' 指定不匹配字符范围
    idx = match(content, /th[^a, \s]/, result);
    if ( idx > 0 ) {
        print("match 6:");
        for( key in result ) {
            print(result[key]);
        }
        print("");
    }

    # '^' 指定字符开头
    idx = match(content, /^I/, result);
    if ( idx > 0 ) {
        print("match 7:");
        for( key in result ) {
            print(result[key]);
        }
        print("");
    }

    # '$' 指定行尾
    idx = match(content, /y$/, result);
    if ( idx > 0 ) {
        print("match 8:");
        for( key in result ) {
            print(result[key]);
        }
        print("");
    }

    # '.' 指定任意字符
    idx = match(content, /h......d/, result);
    if ( idx > 0 ) {
        print("match 9:");
        for( key in result ) {
            print(result[key]);
        }
        print("");
    }

    # '*' 0或多个任意字符
    idx = match(content, /h*d/, result);
    if ( idx > 0 ) {
        print("match 10:");
        for( key in result ) {
            print(result[key]);
        }
        print("");
    }

    # '.*' 贪婪匹配模式
    idx = match(content, /h.*d/, result);
    if ( idx > 0 ) {
        print("match 11:");
        for( key in result ) {
            print(result[key]);
        }
        print("");
    }
}
END{
}

$echo ""|awk -f chapter_3_7-1.awk
match 1:
25
2
so

match 2:
5
1
s

match 3:
20
3
her

match 4:
189
3
5
doing
ing
191

match 5:
124
3
the

match 6:
124
3
the

match 7:
1
1
I

match 8:
833
1
y

match 9:
219
8
had told

match 10:
28
1
d

match 11:
18
814
h her, so deeply in love that I didn’t care if she was sick. I didn’t care that we wouldn’t have long together. None of those things mattered to me. All I cared about was doing something that my heart had told me was the right thing to do. In my mind it was the first time God had ever spoken directly to me, and I knew with certainty that I wasn’t going to disobey. I know that some of you may wonder if I was doing it out of pity. Some of the more cynical may even wonder if I did it because she’d be gone soon anyway and I wasn’t committing much. The answer to both questions is no. I would have married Jamie Sullivan no matter what happened in the future. I would have married Jamie Sullivan if the miracle I was praying for had suddenly come true. I knew it at the moment I asked her, and I still know it tod

results matching ""

    No results matching ""