Regular Expression(正则表达式)
模式配对,判断一个字符串能否满足配对规则
awk中的正则表达式写在两个//中间
awk '/a.c/{print $0}' data.txt
awk '/abc/{print $0}' data.txt
awk '/^abc/{print $0}' data.txt
awk '/$abc/{print $0}' data.txt
1./abc/
"abc"
"xxabcl"
"xxabcxx1"
"a bc" //不符合要求
2./a.c/ //.表示单一字符
"abc"
"aac"
"acc"
"adc"
"aec"
"a c"
3.转义字符来背锅
/a\.c/
"a.c"
/a\/c/
"a/c"
/a\\c/
"a\c"
/a\?c/
"a?c"
4.^和$
/^abc/
"abc"
"abcxxx"
"abcxxxx1"
"abc..."
"xabc" X
"Aabc" X
/abc$/
"abc"
"xxxabc"
"xxxxabc"
"abcxxx" X
5.[] 表示或
/a[xyz]c/
"axc"
"ayc"
"azc"
"axyc" X
/a[a-z]c/
"aac"
"abc"
"acc"
"adc"
"aAc" X
/a[a-zA-Z]c/
"aac"
"aAc"
"Aaac"
/a[^a-z]c/ 表示不是a-c中的一个字符
"aac" X
"aAc" X
6.*和+
/a*b/ *之前的字符可以出现任意次数
"b1"
"ab"
"aab"
"aaab"
"aaaaaaab"
/a+b/ +前的字符至少出现一次
"ab"
"aab"
"aaab"
"aaaab"
"b" X
7.?
/a?b/
"b1""ab"
8.{}
/ab{3}c/
"abbbc"
/ab{3,4}c/
"abbbc"
"abbbbc"
"abbbbbc" X
/ab{3,10}c/ b出现的次数从3到10次都可以
/ab{3,}c/ 至少出现三个b
"abbbbbbbbdc" X 多了一个d
9.() 括号括起来的要反复出现
/(ab)+c/
"abababababc"
"abc"
"ababc"
"abbc" X
网友评论