正则表达式整理

正则表达式整理

基本正则表达式(BRE)

  • ^ 匹配行开头

  • $ 匹配行结尾

  • ^$ 匹配空行

  • . 匹配一个字符

  • \ 转义字符

  • * 匹配前一个字符0-0次以上

  • .* 匹配任意字符

  • ^.* 匹配开头任意字符

  • .*$ 匹配结尾任意字符

  • [abc] 匹配abc中的任意一个字符

  • [^abc] 匹配非abc中的任意一个字符

[root@gsh ~]# grep "^s" /etc/passwd   
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
[root@gsh ~]# grep "h$" /etc/passwd
root:x:0:0:root:/root:/bin/bash
gsh:x:1000:1000:gsh:/home/gsh:/bin/bash
oldboy:x:1001:1001::/home/oldboy:/bin/bash
gsh1:x:1002:1003::/home/gsh1:/bin/bash
TEST1:x:1099:1004::/home/TEST1:/bin/bash
test:x:1112:1112::/home/test:/bin/bash
[root@gsh /etc]# grep -n "^$" /etc/hosts
5:
[root@gsh /etc]# grep -n ".$" /etc/hosts
1:127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
2:::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
3:10.0.0.101 oldgirl www.gsh.com
4:10.0.0.100 oldboy www.oldboy.com
[root@gsh /etc]# echo "\$PWD"
$PWD
[root@gsh /etc]# grep "o*" /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
[root@gsh /etc]# grep -n ".*" /etc/hosts
1:127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
2:::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
3:10.0.0.101 oldgirl www.gsh.com
4:10.0.0.100 oldboy www.oldboy.com
5:
[root@gsh /etc]# grep -n "^.*" /etc/hosts
1:127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
2:::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
3:10.0.0.101 oldgirl www.gsh.com
4:10.0.0.100 oldboy www.oldboy.com
5:
[root@gsh /etc]# grep -n ".*$" /etc/hosts
1:127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
2:::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
3:10.0.0.101 oldgirl www.gsh.com
4:10.0.0.100 oldboy www.oldboy.com
5:
[root@gsh /etc]# grep -n "[abc]" /etc/hosts
1:127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
2:::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
3:10.0.0.101 oldgirl www.gsh.com
4:10.0.0.100 oldboy www.oldboy.com
[root@gsh /etc]# grep -n "[^abc]" /etc/hosts
1:127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
2:::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
3:10.0.0.101 oldgirl www.gsh.com
4:10.0.0.100 oldboy www.oldboy.com

 

扩展正则表达式(ERE)

  • + 匹配前一个字符一次或者多次

  • ? 匹配前一个字符0次或者1次

  • [:/]+ 匹配前一个字符为:或者/ 一次或者多次

  • () /n 反向引用

  • a{n,m} 匹配字符a至少出现n次,至多出现m次

  • a{n} 匹配字符a出现n次

  • a{,m} 匹配字符a至多出现m次

  • a{n,} 匹配字符a至少出现n次

  • | 或者的含义

[root@gsh ~]# cat test2.txt            
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
      inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255
      inet6 fe80::3dbf:a79d:a1e4:f44c prefixlen 64 scopeid 0x20<link>
      ether 00:0c:29:60:21:c3 txqueuelen 1000 (Ethernet)
      RX packets 23463 bytes 2158972 (2.0 MiB)
      RX errors 0 dropped 0 overruns 0 frame 0
      TX packets 15927 bytes 3985098 (3.8 MiB)
      TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@gsh ~]# sed -rn '/10+/p' test2.txt
      inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255
      ether 00:0c:29:60:21:c3 txqueuelen 1000 (Ethernet)
[root@gsh ~]# sed -r '/10?/d' test2.txt
      RX errors 0 dropped 0 overruns 0 frame 0
      TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@gsh ~]# sed -rn '/10?/p' test2.txt
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
      inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255
      inet6 fe80::3dbf:a79d:a1e4:f44c prefixlen 64 scopeid 0x20<link>
      ether 00:0c:29:60:21:c3 txqueuelen 1000 (Ethernet)
      RX packets 23463 bytes 2158972 (2.0 MiB)
      TX packets 15927 bytes 3985098 (3.8 MiB)
[root@gsh ~]# sed -rn 's#^.*inet (.*) netm.*$#\1#gp' test2.txt
10.0.0.100
[root@gsh ~]# awk -F "[a-z ]+" 'NR==2{print $2}' test2.txt
10.0.0.100
[root@gsh ~]# sed -nr '/0{1,2}/p' test2.txt
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
      inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255
      inet6 fe80::3dbf:a79d:a1e4:f44c prefixlen 64 scopeid 0x20<link>
      ether 00:0c:29:60:21:c3 txqueuelen 1000 (Ethernet)
      RX packets 23463 bytes 2158972 (2.0 MiB)
      RX errors 0 dropped 0 overruns 0 frame 0
      TX packets 15927 bytes 3985098 (3.8 MiB)
      TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@gsh ~]# sed -nr '/inet|RX/p' test2.txt
      inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255
      inet6 fe80::3dbf:a79d:a1e4:f44c prefixlen 64 scopeid 0x20<link>
      RX packets 23463 bytes 2158972 (2.0 MiB)
      RX errors 0 dropped 0 overruns 0 frame 0
[root@gsh ~]#

预定义特殊中括号表

正则表达式描述示例
[:alnum:] 匹配任意一个字母或者数字相当于[a-zA-Z0-9] [[:alnum:]]
[:alpha:] 匹配任意一个字母相当于[a-zA-Z] [[:alpha:]]
[:blank:] 匹配空格与制表符(横向与纵向) [[:blank:]]
[:upper:] 匹配大写 [[:upper:]]
[:lower:] 匹配小写 [[:lower:]]
[:punct:] 匹配标点符号 [[:punct:]]
[:space:] 匹配包括换行符、回车在内的所有空白符 [[:space:]]
[:digit:] 匹配任意一个数字相当于[0-9] [[:digit:]]
[:graph:] 匹配任意一个可以看得见且可以打印的字符 [[:graph:]]
[:xdigit:] 匹配任何一个16进制数相当于[0-9][a-f][A-F] [[:xdigit:]]
[:cntrl:] 任何一个控制符(ASCII字符集的前32为字符) [[:cntrl:]]
[:print:] 任何一个可以打印的字符 [[:print:]]
[root@gsh ~]# grep "[[:upper:]]" a
TEST
[root@gsh ~]# grep "[[:lower:]]" a
test
22test1
[root@gsh ~]# grep "[[:alnum:]]" a
test
22test1
123
4 56
7895
TEST
[root@gsh ~]# grep "[[:alpha:]]" a
test
22test1
TEST
[root@gsh ~]# grep "[[:digit:]]" a
22test1
123
4 56
7895
[root@gsh ~]# grep "[[:space:]]" a
22test1
4 56
[root@gsh ~]# cat -A a            
test$
22test1 $
123$
4 56$
7895$
TEST$
[root@gsh ~]#

 

posted @ 2020-09-29 19:57  Gsh-123  阅读(465)  评论(0)    收藏  举报