ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

正则表达式

2022-09-14 22:01:15  阅读:232  来源: 互联网

标签:grep temp 正则表达式 world root hello localhost


正则表达式

目录

基本正则表达式

元字符

. 匹配任意单个字符

[root@localhost ~]# mkdir /temp
[root@localhost temp]# touch {1..9}
[root@localhost temp]# ls | grep '^.$'
1
2
3
4
5
6
7
8
9
#同理,两个点就是任意两个字符

[] 匹配指定范围内的任意单个字符

//匹配2,3,4
[root@localhost temp]# ls | grep '^[234]$'
2
3
4

//匹配2-6
[root@localhost temp]# ls | grep '^[2-6]$'
2
3
4
5
6
//当‘-’在中括号开始和结尾处,表示‘-’本身,例如
[root@localhost temp]# ls | grep '^[-26]$'
2
6
[root@localhost temp]# ls | grep '^[26-]$'
2
6
[root@localhost temp]# ls | grep '^[-26-]$'
2
6

[^] 匹配指定范围外的任意单个字符

//匹配2,6外的任意单个字符
[root@localhost temp]# ls | grep '^[^26]$'
1
3
4
5
7
8
9
//‘^’必须在中括号的开始,否则表示‘^’本身,例如
[root@localhost temp]# ls | grep '^[2^6^]$'
2
6

匹配次数(贪婪模式)

* 匹配器前面的任意单个字符任意次(0到正无穷次)

//‘*’前面的单个字符,也就是b,可以没有一次,也可以有任意次
[root@localhost temp]# ls | grep '^ab*c$'
abbbc
abc
ac
[root@localhost temp]# ls | grep '^ab*$'
a
ab
abb
abbb

.* 任意长度的任意字符

[root@localhost temp]# ls | grep '.*'
1
2
3
4
5
6
7
8
9
a
ab
abb
abbb
abbbc
abc
ac

? 匹配其前面任意单个字符0次或1次

[root@localhost temp]# ls | grep '^ab\?$'
a
ab
[root@localhost temp]# ls | grep '^ab\?c$'
abc
ac

\+ 匹配其前面任意单个字符至少一次

[root@localhost temp]# ls | grep '^ab\+$'
ab
abb
abbb
[root@localhost temp]# ls | grep '^ab\+c$'
abbbc
abc

\{m,n} 匹配其前面任意单个字符至少m次,至多n次

//匹配\{1,2\}前面的单个字符,也就是b,至少一次,至多2次
[root@localhost temp]# ls | grep '^ab\{1,2\}$'
ab
abb

//只匹配2次
[root@localhost temp]# ls | grep '^ab\{2\}$'
abb

//只匹配3次
[root@localhost temp]# ls | grep '^ab\{3\}$'
abbb

位置锚定

^ 锚定行首,此字符后面的任意单个字符必须出现在行首

[root@localhost temp]# touch jjabc aabc abccld

[root@localhost temp]# ls | grep '^a'
a
aabc
ab
abb
abbb
abbbc
abc
abccld
ac
[root@localhost temp]# ls | grep '^j'
jjabc

$ 锚定行尾,此字符前面的任意单个字符必须出现在行尾

[root@localhost temp]# ls | grep 'b$'
ab
abb
abbb
[root@localhost temp]# ls | grep 'c$'
aabc
abbbc
abc
ac
jjabc

^$ 空白行

[root@localhost temp]# cat > a << EOF
> hello world
> 
> nihao shijie
> EOF
[root@localhost temp]# cat a
hello world

nihao shijie
//过滤文本中的空白行
[root@localhost temp]# cat a | grep -v '^$'		//-v是grep命令中取反的意思
hello world
nihao shijie

\<或\b 锚定词首,其后面的任意字符必须作为单词的词首

[root@localhost temp]# cat a
hello world hello tom hello asdfa asdhello gjh
      hello world nihao shijie
hi world hello world worldhello helloasdg


[root@localhost temp]# cat a | grep '\<hello'
hello world hello tom hello asdfa asdhello gjh
      hello world nihao shijie
hi world hello world worldhello helloasdg

[root@localhost temp]# cat a | grep '\bhello'
hello world hello tom hello asdfa asdhello gjh
      hello world nihao shijie
hi world hello world worldhello helloasdg

\>或\b 锚定词尾,其前面的任意字符必须作为单词的词首

[root@localhost temp]# cat a | grep 'hello\>'
hello world hello tom hello asdfa asdhello gjh
      hello world nihao shijie
hi world hello world worldhello helloasdg

[root@localhost temp]# cat a | grep 'hello\b'
hello world hello tom hello asdfa asdhello gjh
      hello world nihao shijie
hi world hello world worldhello helloasdg

分组

\(\)

[root@localhost temp]# touch ab abab ababab aabb abb aab

[root@localhost temp]# ls | grep '^\(ab\)*$'
ab
abab
ababab

后向引用

\1 引用第一个左括号以及与之对应的右括号所包括的所有内容

\2 引用第二个左括号以及与之对应的右括号所包括的所有内容

[root@localhost temp]# cat a 
hello world hello tom hello asdfa asdhello gjh
      hello world nihao shijie
hi world hello world worldhello helloasdg

[root@localhost temp]# cat a | sed 's/hello \(.*\) \(.*\)/hello \2 \1/g'
hello gjh world hello tom hello asdfa asdhello
      hello shijie world nihao
hi world hello helloasdg world worldhello


[root@localhost temp]# cat a | sed 's/hello \(world\) \(hello\)/hello \2 \1/g'
hello hello world tom hello asdfa asdhello gjh
      hello world nihao shijie
hi world hello world worldhello helloasdg

扩展正则表达式

字符匹配

. 匹配任意单个字符

[] 匹配指定范围内的任意单个字符

[^] 匹配指定范围外的任意单个字符

次数匹配

* 匹配其前面的任意单个字符任意次

? 匹配其前面的任意单个字符1次或0次

[root@localhost temp]# ls | grep -E '^ab?$'		//grep -E 是使用扩展正则表达式
a
ab
[root@localhost temp]# ls | grep -E '^ab?$c'
[root@localhost temp]# ls | grep -E '^ab?c$'
abc
ac

+ 匹配其前面的任意单个字符至少1次

[root@localhost temp]# ls | grep -E '^ab+$'
ab
abb
abbb
[root@localhost temp]# ls | grep -E '^ab+c$'
abbbc
abc

{m,n} 匹配其前面的任意单个字符至少m次,至多n次

//匹配{1,2}前面的单个字符,也就是b,至少一次,至多2次
[root@localhost temp]# ls | grep -E '^ab{1,2}$'
ab
abb

//只匹配2次
[root@localhost temp]# ls | grep -E '^ab{2}$'
abb

//只匹配3次
[root@localhost temp]# ls | grep -E '^ab{3}$'
abbb

位置锚定

^ 锚定行首,此字符后面的任意单个字符必须出现在行首

$ 锚定行尾,此字符前面的任意单个字符必须出现在行尾

^$ 空白行

\<或\b 锚定词首,其后面的任意字符必须作为单词的词首

\>或\b 锚定词尾,其前面的任意字符必须作为单词的词首

分组

()

[root@localhost temp]# ls | grep -E '^(ab)*$'
ab
abab
ababab

后向引用

[root@localhost temp]# cat a
hello world hello tom hello asdfa asdhello gjh
      hello world nihao shijie
hi world hello world worldhello helloasdg

[root@localhost temp]# cat a | sed -r 's/hello (.*) (.*)/hello \2 \1/g'		//sed -r使用扩展正则表达式
hello gjh world hello tom hello asdfa asdhello
      hello shijie world nihao
hi world hello helloasdg world worldhello

[root@localhost temp]# cat a | sed -r 's/hello (.*) (.*) (.*)/hello \2 \3 \1/g'
hello asdhello gjh world hello tom hello asdfa
      hello nihao shijie world
hi world hello worldhello helloasdg world

[root@localhost temp]# cat a | sed -r 's/hello (world) (hello)/hello \2 \1/g'
hello hello world tom hello asdfa asdhello gjh
      hello world nihao shijie
hi world hello world worldhello helloasdg

或者

| or 默认匹配|的整个左侧或者整个右侧的内容

[root@localhost temp]# cat a | grep -E 'hello|hi'
hello world hello tom hello asdfa asdhello gjh
      hello world nihao shijie
hi world hello world worldhello helloasdg

标签:grep,temp,正则表达式,world,root,hello,localhost
来源: https://www.cnblogs.com/zicnotes/p/16694722.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有