标签:查找 特殊 字母 脚本 文本 处理 查询 文本处理 扩展正则

本篇主要写一些shell脚本正则表达式的使用基础。
正则表达式分为基础正则表达式(Regular Expression)与扩展正则表达式(Extended Regular Expression)。
它不是一个工具程序,而是一个字符串处理的标准依据,是使用单个字符串搜索、匹配一系列符合某个语法规则的字符串。
它由普通字符(a~z)以及特殊字符(元字符)组成。
| 文本处理工具 | 基础正则表达式 | 扩展正则表达式 |
|---|---|---|
| vi编辑器 | 支持 | / |
| grep | 支持 | / |
| egrep | 支持 | 支持 |
| sed | 支持 | / |
| awk | 支持 | 支持 |
^:匹配输入字符串的开始位置。在方括号表达式中使用,表示不包含该字符集合。要匹配^字符本身,使用\^
$:匹配输入字符串的结尾位置。如果设置了RegExp对象的Multiline属性,则$也匹配\n或\r。要匹配$字符本身,请使用\$
.:匹配除\r\n之外的任何单个字符
\:将下一个字符标记为特殊字符、原义字符、向后引用、八进制转义符。例如,n匹配字符n。 \n匹配换行符。序列\\匹配\,而\(则匹配(
*:匹配前面的子表达式零次或多次。要匹配*字符,请使用\*
[]:字符集合。匹配所包含的任意一个字符。例如,[abc]可以匹配plain中的a
[^]:赋值字符集合。匹配未包含的一个任意字符。例如,[^abc]可以匹配plain中plin中的任何一个字母
[n1-n2]:字符范围。匹配指定范围内的任意一个字符。例如,[a-z]可以匹配a到z范围内的任意一个小写字母字符。
{n}:n是一个非负整数,匹配确定的n次。例如,o{2}不能匹配Bob中的o,但是能匹配food中的两个o
{n,}:n是一个非负整数,至少匹配n次。例如,o{2,}不能匹配Bob中的o,但能匹配foooood中的所有o。o{1,}等价于o+。o{0,}则等价于o*
{n,m}:m和n均为非负整数,其中n<=m,最少匹配n次且最多匹配m次
grep做示例,首先准备一个测试文件he was short and fat.
He was wearing a blue polo shirt with black pants.
The home of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12!
google is the best tools for search keyword.
The year ahead will test our political establishment to the limit.
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words
#woood #
#woooooood #
AxyzxyzxyzxyzC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
-n:显示行号
-i:不区分大小写
-v:反向选择
[root@localhost ~]# grep -n 'the' test.txt
4:the tongue is boneless but it breaks bones.12!
5:google is the best tools for search keyword.
6:The year ahead will test our political establishment to the limit.
[root@localhost ~]# grep -in 'the' test.txt
3:The home of Football on BBC Sport online.
4:the tongue is boneless but it breaks bones.12!
5:google is the best tools for search keyword.
6:The year ahead will test our political establishment to the limit.
[root@localhost ~]# grep -vn 'the' test.txt
1:he was short and fat.
2:He was wearing a blue polo shirt with black pants.
3:The home of Football on BBC Sport online.
7:PI=3.141592653589793238462643383249901429
8:a wood cross!
9:Actions speak louder than words
10:
11:
12:#woood #
13:#woooooood #
14:AxyzxyzxyzxyzC
15:I bet this place is really spooky late at night!
16:Misfortunes never come alone/single.
17:I shouldn't have lett so tast.
[root@localhost ~]# grep -n 'sh[io]rt' test.txt
1:he was short and fat.
2:He was wearing a blue polo shirt with black pants.
oo[root@localhost ~]# grep -n 'oo' test.txt
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
8:a wood cross!
12:#woood #
13:#woooooood #
15:I bet this place is really spooky late at night!
oo前不是w的字符串[root@localhost ~]# grep -n '[^w]oo' test.txt
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
12:#woood #
13:#woooooood #
15:I bet this place is really spooky late at night!
oo前没有小写字母的字符串[root@localhost ~]# grep -n '[^a-z]oo' test.txt
3:The home of Football on BBC Sport online.
[root@localhost ~]# grep -n '[0-9]' test.txt
4:the tongue is boneless but it breaks bones.12!
7:PI=3.141592653589793238462643383249901429
the为行首的行[root@localhost ~]# grep -n '^the' test.txt
4:the tongue is boneless but it breaks bones.12!
[root@localhost ~]# grep -n '^[a-z]' test.txt
1:he was short and fat.
4:the tongue is boneless but it breaks bones.12!
5:google is the best tools for search keyword.
8:a wood cross!
[root@localhost ~]# grep -n '^[A-Z]' test.txt
2:He was wearing a blue polo shirt with black pants.
3:The home of Football on BBC Sport online.
6:The year ahead will test our political establishment to the limit.
7:PI=3.141592653589793238462643383249901429
9:Actions speak louder than words
14:AxyzxyzxyzxyzC
15:I bet this place is really spooky late at night!
16:Misfortunes never come alone/single.
17:I shouldn't have lett so tast.
[root@localhost ~]# grep -n '^[^a-zA-Z]' test.txt
12:#woood #
13:#woooooood #
.结尾的行,需要使用转义字符[root@localhost ~]# grep -n '\.$' test.txt
1:he was short and fat.
2:He was wearing a blue polo shirt with black pants.
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
6:The year ahead will test our political establishment to the limit.
16:Misfortunes never come alone/single.
17:I shouldn't have lett so tast.
[root@localhost ~]# grep -n '^$' test.txt
10:
11:
w和d中间有两个字符的字符串[root@localhost ~]# grep -n 'w..d' test.txt
5:google is the best tools for search keyword.
8:a wood cross!
9:Actions speak louder than words
o的字符串,*代表重复前面的一个字符零个或多个[root@localhost ~]# grep -n 'ooo*' test.txt
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
8:a wood cross!
12:#woood #
13:#woooooood #
15:I bet this place is really spooky late at night!
w和d中间有两个至少有一个o的字符串[root@localhost ~]# grep -n 'woo*d' test.txt
8:a wood cross!
12:#woood #
13:#woooooood #
w和d中间可有可无的字符串[root@localhost ~]# grep -n 'w.*d' test.txt
1:he was short and fat.
5:google is the best tools for search keyword.
8:a wood cross!
9:Actions speak louder than words
12:#woood #
13:#woooooood #
[root@localhost ~]# grep -n '[0-9][0-9]*' test.txt
4:the tongue is boneless but it breaks bones.12!
7:PI=3.141592653589793238462643383249901429
o的字符串,需要转义[root@localhost ~]# grep -n 'o\{2\}' test.txt
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
8:a wood cross!
12:#woood #
13:#woooooood #
15:I bet this place is really spooky late at night!
w和d中间包含2~5个o的字符串[root@localhost ~]# grep -n 'wo\{2,5\}d' test.txt
8:a wood cross!
12:#woood #
w和d中间包含两个以上o的字符串[root@localhost ~]# grep -n 'wo\{2,\}d' test.txt
8:a wood cross!
12:#woood #
13:#woooooood #
+:重复一个或者一个以上的前一个字符
?:零个或者一个的前一个字符
|:使用或者or的方式找出多个字符
():查找组字符串
()+:辨别多个重复的组
rgrep做示例,查询w和d之间包含一个以上o的字符串[root@localhost ~]# egrep -n 'wo+d' test.txt
8:a wood cross!
12:#woood #
13:#woooooood #
bet和best这两个字符串[root@localhost ~]# egrep -n 'bes?t' test.txt
5:google is the best tools for search keyword.
15:I bet this place is really spooky late at night!
of或者if或者on字符串[root@localhost ~]# egrep -n 'of|is|on' test.txt
3:The home of Football on BBC Sport online.
4:the tongue is boneless but it breaks bones.12!
5:google is the best tools for search keyword.
6:The year ahead will test our political establishment to the limit.
9:Actions speak louder than words
15:I bet this place is really spooky late at night!
16:Misfortunes never come alone/single.
tast或者test字符串[root@localhost ~]# egrep -n 't(a|e)st' test.txt
6:The year ahead will test our political establishment to the limit.
17:I shouldn't have lett so tast.
A结尾是C,中间有一个以上的xyz字符串[root@localhost ~]# egrep -n 'A(xyz)+C' test.txt
14:AxyzxyzxyzxyzC标签:查找 特殊 字母 脚本 文本 处理 查询 文本处理 扩展正则
原文地址:https://www.cnblogs.com/llife/p/11675283.html