加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式

发布时间:2020-12-13 19:34:00 所属栏目:百科 来源:网络整理
导读:^行首定位符 $行尾定位符 #mkdir/test #cd/test [root@teachertest]#cp/etc/passwd./ #grep"root"passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@teachertest]#grep"^root"passwd root:x:0:0:root:/root:/bin/

^行首定位符
$行尾定位符

#mkdir/test
#cd/test
[root@teachertest]#cp/etc/passwd./

#grep"root"passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

[root@teachertest]#grep"^root"passwd
root:x:0:0:root:/root:/bin/bash

[root@teachertest]#grep"bash$"passwd

root:x:0:0:root:/root:/bin/bash

#vimpasswd
添加
ro:x:0:0:root:/root:/bin/bash

[root@teachertest]#grep"^ro"passwd
root:x:0:0:root:/root:/bin/bash
ro:x:0:0:root:/root:/bin/bash
[root@teachertest]#grep"^ro:"passwd
ro:x:0:0:root:/root:/bin/bash

[root@teachertest]#grep"sh$"passwd

----------------------
.匹配换行字符之外的任意单个字符
*匹配前面的字符,有0个或多个

[root@teachertest]#vims1

abc
acc
adc
qbc
wbc
ebc

[root@teachertest]#grep"a.c"s1
abc
acc
adc

[root@teachertest]#grep"..c"s1
abc
acc
adc
qbc
wbc
ebc

#grep"a*bc"s1
abc
qbc
wbc
ebc

[root@teachertest]#grep--color"a*bc"s1
abc
qbc
wbc
ebc

#grep--color".*:"passwd
-----------------------------
[...]匹配字符组内任意一个字符
[0-9]0123456789
[a-z]小写字母
[A-Z]大写字母
[a-Z]所有字母
[a-Z0-9]字母或数字
[^...]取反,非字符组里的任意一个字符
[^0-9]非数字
[^a-Z]非字母


#grep"^[a-z][a-z][a-z]"passwd

[root@teachertest]#grep--color"^[a-z][a-z][a-z]:"passwd

#grep"x:[1-9][0-9]:"passwd--color

#grep":[1-9][0-9]:[0-9]"passwd--color
-----------------------------
屏蔽元字符的含义meta

#vims1

abc
acc
adc
qbc
wbc
ebc
e.c

[root@teachertest]#grep"e.c"s1
ebc
e.c
[root@teachertest]#grep"e.c"s1
e.c

#cp/etc/shadow./
[root@teachertest]#grep'$'shadow
----------------------------------
()标签,保存标签里面的字符,使用1-9调用,最多9个标签,排列顺序,从左向右。
(1)...(2)...(3)123

#grep"(root).*1"passwd--color
root:x:0:0:root:/root:/bin/bash
ro:x:0:0:root:/root:/bin/bash
Youhavenewmailin/var/spool/mail/root
[root@teachertest]#grep"(root).*1.*1"passwd--color
root:x:0:0:root:/root:/bin/bash
[root@teachertest]#grep"(root).*(0).*2.*1"passwd--color
root:x:0:0:root:/root:/bin/bash
Youhavenewmailin/var/spool/mail/root

[root@teachertest]#vima1

root,/:tom,:root:...tom:,mike:,root,:mike,:tom,:root

#grep"(root).*(tom).*1.*2.*(mike).*1.*3.*3.*2.*1"a1
---------------
#vims1

abc
acc
adc
qbc
wbc
ebc
e.c
mikeandtom
把tom和mike的位置对调过来

:1,$s/(tom)and(mike)/2and1/g

#vims1
abc
acc
adc
qbc
wbc
ebc
e.c
mikeandtom
aaab
cccd
1112
3334

[root@teachertest]#grep"(.)11"s1
aaab
cccd
1112
3334

----------------------------
x{m}x(任意字符)有m个
x{m,}x(任意字符)有>m个
x{m,n}x(任意字符)有m~n个

[root@teachertest]#grep"^[a-z][a-z][a-z]:"passwd--color

#grep"^[a-z]{3}:"passwd--color

[root@teachertest]#grep"^[a-z]{3,}:"passwd--color

#grep"^[a-z]{3,4}:"passwd--color

------------------------------
&;词首定位符
&;词尾定位符

#vimpasswd
添加
roooot:x:0:0:root:/root:/bin/bash
rootooo:x:0:0:bin/bash:arooootbc

[root@teachertest]#grep"^&;root"passwd--color

[root@teachertest]#grep"^&;ro"passwd--color

[root@teachertest]#grep"^&;ro&;"passwd--color
ro:x:0:0:root:/root:/bin/bash

[root@teachertest]#grep"&;root&;"passwd--color

[root@teachertest]#grep"ooot&;"passwd--color
-------------------------

POSIX表示法
portableoperatingsysteminterface
可移植操作系统接口
[:alpha:]字母a-Z[a-Z]
[:lower:]小写字母a-z
[:upper:]大写字母A-Z
[:digit:]十进制数0-9
[:xdigit:]十六进制数0-9a-F
[:alnum:]字母+数字a-Z0-9
[:space:]空白字符(空格tab)
[:punct:]符号
[^a-Z0-9]?
[^[:alpha:]]取反

#vims1
mikeandtom
aaab
cccd
1112
3334
abcdefg//使用space键加空格
hello//使用tab键加空格

#grep'^[[:space:]]'s1
#grep'[[:punct:]]'s1--color

-------------------------------
扩展正则

egrep=grep-E
支持的基本正则元字符^$.*[]<&;

|或

#egrep'NW|WE'datafile
northwestNWCharlesMain3.0.98334
westernWESharonGray5.3.97523
westernWESharonGray5.3.97523

#grep-E'NW|WE'datafile
northwestNWCharlesMain3.0.98334
westernWESharonGray5.3.97523
westernWESharonGray5.3.97523

?前面的条件匹配0次或1次

[root@teachertest]#egrep'a?bc's1
abc
qbc
wbc
ebc
abcdefg

---------------------
+前面的条件匹配一次或多次
#vims1
abcabcabc
[root@teachertest]#egrep'a+bc's1
abc
abcdefg

[root@teachertest]#egrep'(abc)+'s1--color

[root@teachertest]#egrep'aa+'s1
aaab

#egrep'^[a-z]+:'passwd--color

------------------------
()标签或一组条件

#egrep'(root).*1.*1'passwd--color
root:x:0:0:root:/root:/bin/bash

#vims2
at
atat
atatat
aataataat

#egrep'(at){3}'s2--color
atatat

-----------------------
x{m}x{m,}x{m,n}条件重复多少次

#egrep'(at){2,3}'s2--color
atat
atatat
-----------------------
fgrep

[root@teachertest]#fgrep'e.c's1

[root@teachertest]#fgrep'$'s1
,%$?+-)(

-----------------------------
grep
-i不去分大小写
-v取反
-c匹配的行数
-n显示行号
-o只显示匹配内容
--color色彩显示匹配的内容
-r递归查找

#cd/test
#grep-r'root'/test

-l//当于r选项结合使用时把包含正则表达式的文件名列出
#grep-rl'root'/test
/test/passwd
/test/loginfile
/test/a1
/test/shadow

-Anum显示匹配内容下num行
-Bnum显示匹配内容上num行
-num显示匹配内容上下num行

#vims3
1
2
3
4abc
5
6
7
8
9

[root@teachertest]#grep-A2'abc's3
4abc
5
6
[root@teachertest]#grep-B2'abc's3
2
3
4abc
Youhavenewmailin/var/spool/mail/root
[root@teachertest]#grep-2'abc's3
2
3
4abc
5
6

#cat-npasswd
1root:x:0:0:root:/root:/bin/bash
2roooot:x:0:0:root:/root:/bin/bash
3rootooo:x:0:0:/bin/bash:arooootbc
4ro:x:0:0:root:/root:/bin/bash
5bin:x:1:1:bin:/bin:/sbin/nologin
6daemon:x:2:2:daemon:/sbin:/sbin/nologin
7adm:x:3:4:adm:/var/adm:/sbin/nologin
8lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
9sync:x:5:0:sync:/sbin:/bin/sync
10shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
11halt:x:7:0:halt:/sbin:/sbin/halt
12mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
13news:x:9:13:news:/etc/news:
14uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
15operator:x:11:0:operator:/root:/sbin/nologin
16games:x:12:100:games:/usr/games:/sbin/nologin
17gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
18ftp:x:14:50:FTPUser:/var/ftp:/sbin/nologin
19nobody:x:99:99:Nobody:/:/sbin/nologin
20nscd:x:28:28:NSCDDaemon:/:/sbin/nologin


[root@teachertest]#cat-npasswd|egrep'[[:space:]]+1'
1root:x:0:0:root:/root:/bin/bash
10shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
11halt:x:7:0:halt:/sbin:/sbin/halt
12mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
13news:x:9:13:news:/etc/news:14uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin15operator:x:11:0:operator:/root:/sbin/nologin16games:x:12:100:games:/usr/games:/sbin/nologin17gopher:x:13:30:gopher:/var/gopher:/sbin/nologin18ftp:x:14:50:FTPUser:/var/ftp:/sbin/nologin19nobody:x:99:99:Nobody:/:/sbin/nologin

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读