【问题标题】:How to find string content in between start and end of string using preg match如何使用 preg 匹配在字符串的开头和结尾之间查找字符串内容
【发布时间】:2018-05-07 18:35:29
【问题描述】:

我有以下字符串

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 地址”字符串后跟“ip vrf 转发”即可。

所以按照上述条件:

  • 我应该得到 Access-Mgmt、IN_MEDIA_SIGNALING、Atheeb-VoIP、V3620:EBU-COMIS 等等。
  • 我不应该得到 V1373:SAMIS(因为后面没有 ip 地址)

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

【问题讨论】:

  • ip vrf forwarding (\S+)\s*ip address (.*) 尚不清楚您实际上要获取什么,但这对我有用。
  • 实际上我想将具有 ip 地址的 vrf 名称显示为活动的,而那些没有 IP 地址的则显示为非活动的

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


【解决方案1】:

你可以使用

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

【讨论】:

  • OP 似乎想要输出两个地址,但我可能是错的。如果是这种情况,将 [\d.] 替换为 [\d. ] 可以解决问题(并在空格上拆分字符串)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-06
  • 1970-01-01
  • 1970-01-01
  • 2021-11-28
相关资源
最近更新 更多