正则表达式
1.正则表达式:
(1)概述:
-
针对文本内容,用于字符串的快速查找,定位,替换,删除等;
-
在很多文本编辑器里,正则表达式通常被用来检索、替换那些匹配某个模式的文本;
-
在linux中,通配符是由shell解释的,而正则表达式则是由命令解释的;
-
正则表达式由两种基本字符类型组成:原义文本字符(正常)和元字符(特殊);
-
通常配合grep、sed、awk、vim等支持正则表达式的工具程序使用;
(2)正则表达式与通配符区别:
① 通配符:是shell在做匹配的时候会用到,一般用于匹配文件名。
② 正则表达式:就是为了处理大量的文本和字符串而定义的一套规则和方法。
- 涉及grep/egrep、sed、awk都是正则表达式;其他的都是通配符;
- 涉及文本内容都是正则表达式;涉及文件和目录名都是通配符;
(3)基本正则表达式(BRE):
grep在使用?、+、{m,n}、()时,需要将这些字符转义,即在这些字符前加上“\”转义字符。
1)字符匹配:
-
.:匹配除\n以外的任意符号
-
[]:匹配指定范围内任意单个字符
-
[^]:匹配指定范围外任意单个字符
-
[:alnum:]:字母与数字字符,等于[a-z][A-Z][0-9]
-
[:alpha:]:字母,等于[a-z][A-Z]
-
[:upper:]:大写字母,等于[A-Z]
-
[:lower:]:小写字母,等于[a-z]
-
[:digit:]:数字,等于[0-9]
-
[:xdigit:]:十六进制数字
-
[:punct:]:特殊符号字符,例如!,@,#,$等
-
[:space:]:空白字符,包括垂直制表符、空格、tab、换行、分页符等
-
[:blank:]:空格或制表符
-
[:graph:]:非控制、非空格字符
-
[:ascii:]:ASCII字符
-
[:cntrl:]:ASCII控制字符
-
[:print:]:可打印字符
2)字符匹配次数:
-
*:匹配前面的字符任意次数
-
.*:匹配任意长度的字符
-
\?:匹配前面的字符0或1次
-
\+:匹配前面的字符至少1次
-
\{m\}:匹配前面的字符m次
-
\{m,n\}:匹配前面的字符至少m次,最大n次
-
\{0,n\}:匹配前面的字符至多n次
-
\{m,\}:匹配前面的字符至少m次
[root@localhost ~]# grep -n "he*" ./1.txt #匹配./1.txt文件里*号前面跟h和e任意次数的字符串
1:hello world
2:hello python
[root@localhost ~]# grep -n "he*l" ./1.txt #匹配./1.txt文件里字母l前面跟字母h和e任意次数的字符串
1:hello world
2:hello python
[root@localhost ~]# grep -n "he.*" ./1.txt #匹配./1.txt文件里以字母h和e开头的所有字符串
1:hello world
2:hello python
[root@localhost ~]# grep -n "he.*l" ./1.txtt #匹配./1.txt文件里以字母h和e开头,以l结尾任意次数的字符串
1:hello world
2:hello python
[root@localhost ~]# grep -n "he\?" ./1.txt #匹配./1.txt文件里任意以字母h和e开头出现0或1次数的字符串
1:hello world
2:hello python
[root@localhost ~]# grep -n "he\?l" ./1.txt #匹配./1.txt文件里字母l前面以字母h和e开头出现0或1次数的字符串
1:hello world
2:hello python
[root@localhost ~]# grep -n "he\+" ./1.txt #匹配./1.txt文件里至少一次以字母h和e开头的字符串
1:hello world
2:hello python
[root@localhost ~]# grep -n "o\{0,2\}" ./1.txt #匹配./1.txt文件里出现0次到2次字母o的字符串
1:hello world
2:hello python
……
3)位置锚定:
-
^:行首锚定,用于模式的最左侧
-
$:行尾锚定,用于模式的最右侧
-
^PATTERN$:用于模式匹配整行内容
-
^$:匹配空行
-
\b | \<:词首锚定,用于单词模式的左侧
-
\b | \>:词尾锚定,用于单词模式的右侧
-
\<PATTERN\>:匹配整个单词
[root@localhost ~]# grep -n "^the" /tmp/greptest #匹配/tmp/greptest文件里面行首是the开头的行
12:the symbol '*' is represented as start.
24:the kiio pooo
[root@localhost ~]# grep -n "ww$" /tmp/greptest #匹配/tmp/greptest文件里以ww结尾的行
26:these iisufu www
[root@localhost ~]# grep -n "^Th.*.$" /tmp/greptest #匹配/tmp/greptest文件里以Th开头的行
11:This window is clear.
14:The gd software is a library for drafting programs.
16:The world <Happy> is the same with "glad".
[root@localhost ~]# grep -n '^$' /tmp/functions #匹配空白行
……
4)分组:
-
\(...\):将一个或多个字符捆绑在一起,当作一个整体进行处理
\(xy\)
*ab:文本中xy出现任意次数和ab出现任意次数的字符
\(xy\)
+ab:文本中xy出现任意次数和ab出现最少1次的字符
\1:从最左侧起,第一个括号中匹配到的内容
\2:从最左侧起,第二个括号中匹配到的内容
……
[root@localhost ~]# grep "\(ab\+\(xy\)\)" /root/1.txt
\1:ab\+\(xy\)
\2:xy
(4)扩展正则表达式(ERE):
egrep在使用?、+、{m,n}、()时,不需要将这些字符转义。
1)字符匹配:
同上述基本正则表达式一样
2)匹配次数:
-
*:匹配前面的字符任意次数
-
.*:匹配任意长度的字符
-
?:匹配前面的字符0或1次
-
+:匹配前面的字符至少1次
-
{m}:匹配前面的字符m次
-
{m,n}:匹配前面的字符至少m次,最大n次
-
{0,n}:匹配前面的字符至多n次
-
{m,}:匹配前面的字符至少m次
3)位置锚定:
-
^:行首锚定,用于模式的最左侧
-
$:行尾锚定,用于模式的最右侧
-
^PATTERN$:用于模式匹配整行内容
-
^$:匹配空行
-
\b | <:词首锚定,用于单词模式的左侧
-
\b | >:词尾锚定,用于单词模式的右侧
-
<PATTERN>:匹配整个单词