如何使用preg匹配在字符串的开头和结尾之间查找字符串内容

时间:2017-11-23 18:22:16

标签: php regex preg-match preg-match-all

我有一个跟随字符串

interface Vlan17
 description ALU-VDSL-MGM
 ip vrf forwarding Access-Mgmt
 ip address 10.156.207.254 255.255.255.192
!
interface Vlan18
 description name VDSL-VOIP
 ip vrf forwarding IN_MEDIA_SIGNALING
 ip address 20.11.64.1 255.255.240.0
 ip helper-address 10.200.15.39
 ip helper-address 10.208.10.199
!
interface Vlan24
 description By NCM: Task ID 49899 - 24-0 -
 no ip address
!
interface Vlan30
 description By NCM: Task ID 149868 - HU-FTTH-MNG-HC111003OL05
 ip vrf forwarding Access-Mgmt
 ip address 10.155.225.214 255.255.255.248
 no ip redirects
!
interface Vlan39
 description ALU-NGN-MNG
 ip vrf forwarding Access-Mgmt
 ip address 10.202.23.252 255.255.255.0
!
interface Vlan58
 description By NCM: Task ID 571151 - HU-NGN-MNG-395-32-000
 ip vrf forwarding Access-Mgmt
 ip address 10.143.21.254 255.255.255.240
 no ip redirects
!
interface Vlan72
 no ip address
 shutdown
!
interface Vlan75
 description By NCM: Task ID 536967 - Atheeb-VOIP
 ip vrf forwarding Atheeb-VoIP
 ip address 10.248.92.1 255.255.252.0
 ip helper-address 172.16.89.22
 ip helper-address 172.17.89.22
 no ip redirects
 no ip unreachables
 no ip proxy-arp
!
interface Vlan78
 description By NCM: Task ID 049989 - 78-0 -
 no ip address
!
interface Vlan81
 description By NCM: Task ID 4498508 - MW-Mgmt
 ip vrf forwarding V3620:EBU-COMIS
 ip address 10.153.207.193 255.255.255.224
!
interface Vlan82
 description By NCM: Task ID 606871 - MDU-Mgmt
 ip vrf forwarding V3620:EBU-COMIS
 ip address 10.74.37.193 255.255.255.192 secondary
 ip address 10.74.117.1 255.255.255.192
 no ip redirects
!
interface Vlan83
 description By NCM: Task ID 790456 - PtMP-Mgmt
 ip vrf forwarding PtMP-Mgmt
 ip address 10.73.3.78 255.255.255.248
 no ip redirects
 no ip unreachables
!
interface Vlan84
 description By NCM: Task ID 1486 - PtMP-Mgmt
 ip vrf forwarding PtMP-Mgmt
 ip address 10.73.85.129 255.255.255.128
 no ip redirects
 no ip unreachables
!
interface Vlan100
 description ALU-NGN-Signaling
 ip vrf forwarding V1464:STC-NGN-Signalling
 ip address 10.200.23.62 255.255.255.192
 no ip redirects
 no ip unreachables
 no ip proxy-arp
 load-interval 30
!
interface Vlan109
 description By VPNSC: Job Id# = 30690 (SAMIS-RYAD07_00_241-RYAD07_00_241 IP1)
 ip vrf forwarding V1373:SAMIS
 no ip address
 no ip unreachables
 no ip proxy-arp
!

现在我想找到

ip vrf forwarding Access-Mgmt
ip address 10.156.207.254 255.255.255.192

即我想找到' Access-Mgmt'提供“IP地址”#39;字符串后面应该只有' ip vrf forwarding'。

按照上述条件:

  • 我应该获得Access-Mgmt,IN_MEDIA_SIGNALING,Atheeb-VoIP,V3620:EBU-COMIS等。
  • 我不应该得到V1373:SAMIS(因为它没有跟着ip地址)

我们可以在php中使用preg match来获取它吗?

1 个答案:

答案 0 :(得分:0)

您可以使用

Access-Mgmt\D*\b([\d.]+)\b

然后选择小组1,请参阅a demo on regex101.com

<小时/> 细分,这说:

Access-Mgmt   # Access-Mgmt literally
\D*           # not a number 0+ times
\b([\d.]+)\b  # numbers and dots between word boundaries