正则表达式


基本正则表达式

//元字符
    .           //任意单个字符
    []          //匹配指定范围内的任意单个字符
    [^]         //匹配指定范围外的任意单个字符
[root@localhost tmp]# ls
1   2   3   4   5   6   7   8   9   a  c  e  g
1a  2a  3a  4a  5a  6a  7a  8a  9a  b  d  f  h
[root@localhost tmp]# ls | grep '^.$'       
1
2
3
4
5
6
7
8
9
a
b
c
d
e
f
g
h
[root@localhost tmp]# ls | grep '^..$'
1a
2a
3a
4a
5a
6a
7a
8a
9a


[root@localhost tmp]# ls | grep '^4a$'
4a
[root@localhost tmp]# ls | grep '^[4a]$'     
4
a

[root@localhost tmp]# ls | grep '^[^4a]$'           //^只能在最前面才生效
1
2
3
5
6
7
8
9
b
c
d
e
f
g
h
[root@localhost tmp]# ls | grep '^[4a^]$'
4
a

//匹配次数(贪婪模式)
    *           //匹配其前面的任意单个字符任意次
    .*          //任意长度的任意字符
    \?          //匹配其前面的任意单个字符1次或0次
    \+          //匹配其前面的任意单个字符至少1次
    \{m,n\}     //匹配其前面的任意单个字符至少m次,至多n次
[root@localhost tmp]# ls
1   2   3   4   5   6   7   8   9   a      aabc  b   c  e  g
1a  2a  3a  4a  5a  6a  7a  8a  9a  aaabc  abc   bc  d  f  h
[root@localhost tmp]# ls | grep '^a*bc$'            
aaabc
aabc
abc
bc
[root@localhost tmp]# ls | grep '^a.*$'   //以a为开头的所有文件
a
aaabc
aabc
abc
[root@localhost tmp]# ls | grep '^.*$'    //全部的意思
......

[root@localhost tmp]# ls | grep '^a\?bc$'
abc
bc
[root@localhost tmp]# ls | grep '^a\+bc$'
aaabc
aabc
abc

[root@localhost tmp]# ls | grep '^a\{2,5\}bc$'
aaabc
aabc

//位置锚定
    ^           //锚定行首,此字符后面的任意单个字符必须出现在行首
    $           //锚定行尾,此字符前面的任意单个字符必须出现在行尾
    ^$          //空白行
    \<或\b       //锚定词首,其后面的任意单个字符必须作为单词首部出现
    \>或\b       //锚定词尾,其前面的任意单个字符必须作为单词尾部出现
[root@localhost tmp]# ls | grep 'a'
1a
2a
3a
4a
5a
6a
7a
8a
9a
a
aaabc
aabc
ab
abc
[root@localhost tmp]# ls | grep '^a'
a
aaabc
aabc
ab
abc

[root@localhost tmp]# ls | grep 'a$'
1a
2a
3a
4a
5a
6a
7a
8a
9a
a

[root@localhost tmp]# ls | grep '^a$'
a

[root@localhost tmp]# cat z
hello word hello j 

dwaio dwa
[root@localhost tmp]# cat z |grep '^$'
             
[root@localhost tmp]# cat z |grep '[^^#]'
hello word hello j 
dwaio dwa
[root@localhost tmp]# cat z
hello word hello j 

hellozdz  
llhello
dwaio dwa
[root@localhost tmp]# cat z |grep '\<hello'
hello word hello j 
hellozdz  
[root@localhost tmp]# cat z |grep 'hello\>'
hello word hello j 
llhello
[root@localhost tmp]# cat z |grep '\<hello\>'
hello word hello j 

/分组
  \(\)
    例:\(ab\)*
    //后向引用
        \1      //引用第一个左括号以及与之对应的右括号所包括的所有内容
        \2      //引用第二个左括号以及与之对应的右括号所包括的所有内容
[root@localhost tmp]# cat z
hello word hello j 

hellozdz  
llhello
dwaio dwa
[root@localhost tmp]# sed  's/hello \(.*\) \(.*\) /hello \2 \1 /' z
hello j word hello 

hellozdz  
llhello
dwaio dwa

扩展正则表达式

正则表达式与扩展正则表达式类似,只是扩展正则表达式表达更加简单
正则表达式需要打 \ 转义字符 来表示某些字符的意思,而扩展正则表达式则不需要:
? //匹配其前面的任意单个字符1次或0次
+ //匹配其前面的任意单个字符至少1次
{m,n} //匹配其前面的任意单个字符至少m次,至多n次
() //分组

[root@localhost tmp]# ls
1  2  3  4  5  6  7  8  9  a  aaab  aab  ab  b  c  d  e
[root@localhost tmp]# ls | grep -E '^a?b$'
ab
b

[root@localhost tmp]# ls | grep -E '^a+b$'
aaab
aab
ab

[root@localhost tmp]# ls | grep -E '^a{2,5}b$'
aaab
aab
[root@localhost tmp]# ls | grep -E '^a{2}b$'
aab
[root@localhost tmp]# ls | grep -E '^a{1}b$'
ab
[root@localhost tmp]# ls | grep -E '^a{3}b$'
aaab

[root@localhost tmp]# cat z
hello hello zxcv hello dwadfg
hello zzz
[root@localhost tmp]# sed -r 's/hello (.*) (.*)/hello \2 \1/' z
hello dwadfg hello zxcv hello
hello zzz



posted @ 2022-09-14 19:29  世界的尽头*  阅读(53)  评论(0)    收藏  举报