在我们实际应用中,正则表达式的应用是非常广泛的,今天我就大家分享几个比较常见的正则表达式的应用实例,不周之处,还望高人多多指点!
一:使用正则表达式搜索邮箱地址
二:使用正则表达式搜索手机号
三:使用正则表达式搜索身份证号
四:使用正则表达式取网络配置文件中的IP地址的过程解析
一:使用正则表达式搜索邮箱地址:
- [root@centos7test]#catmail
- 183530300@qq.com
- netadmin@126.com
- all-user@vip.qq.com
- love_you@51cto.com
- linuxedu@foxmail.com.cn
- love-fire@vip.sina.com
- love@vip.yahoo.com
- ThisisamailBoxaddresstable[root@centos7test]#egrep"\<([[:alnum:]]+(-|_)*[[:alnum:]]*)\>@([[:lower:]]|[[:digit:]])+\.[[:lower:]]*(\.[[:lower:]]+)*"mail
- 183530300@qq.com
- netadmin@126.com
- all-user@vip.qq.com
- love_you@51cto.com
- linuxedu@foxmail.com.cn
- love-fire@vip.sina.com
- love@vip.yahoo.com
- ThisisamailBoxaddresstable
- qq_12345@qq.com
- -NB74110@qq.com
二:使用正则表达式搜索手机号:
- [root@centos7test]#cattel
- 12812345678
- a13012345678
- b13112345678
- c13212345678
- 1331234567890
- 13312345678
- 13412345678
- 13512345678
- 13612345678
- 13712345678
- 13812345678
- 13912345678
- 14012345678
- 14712345678
- 15012345678
- 15112345678
- 15212345678
- 15312345678
- 15412345678
- 15512345678
- 15612345678
- 15712345678
- 15812345678
- 15912345678
- 16012345678
- 17012345678
- 17712345678
- 17812345678
- 18012345678
- 18112345678
- 18212345678
- 18312345678
- 18412345678
- 18512345678
- 18612345678
- 18712345678
- 18812345678
- abc1891234567890def
- 19012345678
- [root@centos7test]#egrep"\<13[0-9]{9}|147[0-9]{8}|15([0-3]|[5-9])[0-9]{8}|17[0,7][0-9]{8}|18([0-3]|[5-9])[0-9]{8}\>"tel
搜索结果如图所示
三:使用正则表达式搜索身份证号:
- [root@centos7test]#catid
- 123456789876543210
- 130626199010016512def
- 130626199010016512
- abc130626199010016512
- 160626199010016512
- 200626199010016515
- 220626199010016515
- 240626199010016515
- 30062619901001651X
- 32062619901001651X
- 38062619901001651X
- 402062620080808100
- 420626198002023518
- 42062619901001651X
- 462062620080808100
- 472062620080808100
- 500626199010016517
- 522122199808081671
- 520626199010016517
- 550626199010016517
- 560626199010016517
- 600626199010016519
- 650626199010016519
- 660626199010016519
- 70162619900231651X
- 71162619901001651X
- 720626199010016510
- 800626199010016511
- 987654328765432165
- 810626199002316511
- 810626201800016511
- 820626199010016511
- 820626101013326511
- 830626100100165119
- 584721520131499999
- [root@centos7test]#egrep"\<((1[1-5])|(2[1-3])|(3[1-7])|(4[1-6])|(5[0-4])|(6[1-5])|(71|81|82))([0-9]){4}(19|20)([0-9]){2}((0[1-9])|(1[0-2]))(0[1-9]|([0-9])|(2[0-9])|(3[0-1]))([0-9]){3}([0-9]|X)\>"id
- 130626199010016512
- 220626199010016515
- 32062619901001651X
- 420626198002023518
- 42062619901001651X
- 500626199010016517
- 522122199808081671
- 520626199010016517
- 650626199010016519
- 71162619901001651X
- 810626199002316511
- 820626199010016511
四:使用正则表达式取网络配置文件中的IP地址的过程解析:
取四段中的第一段
- [root@centos7test]#egrep"\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-1][0-9]|22[0-3])\>."/etc/sysconfig/network-scripts/ifcfg-eno16777728
- IPADDR=10.1.254.254
- GATEWAY=10.1.0.1
- DNS1=202.106.0.20
取四段中的第二段
- [root@centos7test]#egrep"\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-1][0-9]|22[0-3])\>.(\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.)"/etc/sysconfig/network-scripts/ifcfg-eno16777728
- IPADDR=10.1.254.254
- GATEWAY=10.1.0.1
- DNS1=202.106.0.20
取四段中的第三段
- [root@centos7test]#egrep"\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-1][0-9]|22[0-3])\>.(\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.){2}"/etc/sysconfig/network-scripts/ifcfg-eno16777728
- IPADDR=10.1.254.254
- GATEWAY=10.1.0.1
- DNS1=202.106.0.20
取四段中的第四段
- [root@centos7test]#egrep"\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-1][0-9]|22[0-3])\>.(\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.){2}\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\>"/etc/sysconfig/network-scripts/ifcfg-eno16777728
- IPADDR=10.1.254.254
- GATEWAY=10.1.0.1
- DNS1=202.106.0.20
只显示IP地址
- [root@centos7test]#egrep-o"\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-1][0-9]|22[0-3])\>.(\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.){2}\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\>"/etc/sysconfig/network-scripts/ifcfg-eno16777728
- 10.1.254.254
- 10.1.0.1
- 202.106.0.20