在我們實際應用中,正則表達式的應用是非常廣泛的,今天我就大家分享幾個比較常見的正則表達式的應用實例,不周之處,還望高人多多指點!
一:使用正則表達式搜索郵箱地址
二:使用正則表達式搜索手機號
三:使用正則表達式搜索身份證號
四:使用正則表達式取網絡配置文件中的IP地址的過程解析
一:使用正則表達式搜索郵箱地址:
[root@centos7 test]# cat 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 This is a mailbox address table [root@centos7 test]# 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 This is a mailbox address table qq_12345@qq.com -NB74110@qq.com
二:使用正則表達式搜索手機號:
[root@centos7 test]# cat tel 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@centos7 test]# 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@centos7 test]# cat id 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@centos7 test]# 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@centos7 test]# 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@centos7 test]# 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@centos7 test]# 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@centos7 test]# 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@centos7 test]# 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
本文出自 “愛情防火墻” 的博客,請務必保留此出處:http://www.www58058.com/author/lovefirewall
原創文章,作者:愛情防火墻,如若轉載,請注明出處:http://www.www58058.com/29995