首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多行模式的php preg_match_all

多行模式的php preg_match_all
EN

Stack Overflow用户
提问于 2017-10-05 20:12:30
回答 1查看 607关注 0票数 0

我被困在这个问题上,希望任何一个人都能帮我。

我有一个配置文件,其中包含以下行:

代码语言:javascript
复制
config system interface
edit "internal1"
    set vdom "root"
    set ip 192.168.1.1 255.255.255.0
    set allowaccess ping https ssh http fgfm capwap
    set type physical
    set snmp-index 1
next
edit "internal2"
    set vdom "root"
    set ip 192.168.20.2 255.255.255.0
    set allowaccess ping https ssh http fgfm capwap
    set type physical
    set snmp-index 2
    Set secondary-IP enable
      config secondaryip
        edit 1
          set ip 192.168.21.2 255.255.255.0
        next
        edit 2
          set ip 192.168.22.2 255.255.255.0
        next
      end
next
edit "internal3"
    set vdom "root"
    set ip 192.168.30.3 255.255.255.0
    set allowaccess ping https ssh http fgfm capwap
    set type physical
    set snmp-index 3
    Set secondary-IP enable
      config secondaryip
        edit 1
          set ip 192.168.31.3 255.255.255.0
        next
      end
next
end
....

并希望使用以下正则表达式来匹配接口的名称、vdom、vlanid、ip和备用ip:

代码语言:javascript
复制
preg_match_all("/edit .+(\s+config secondaryip\r?\n(\s+edit \d+\r?\n.+\s+next\r?\n){1,}\s+end\r?\n)?.+next\r?\n/s", $configFile, $matched_interfaces);

对于第一个.+,一切都是匹配的,而不是其他的!

如有任何建议

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-05 21:23:42

我喜欢regex

代码语言:javascript
复制
$regex = '/(?<=\vedit ")(\w+)|(?<=vdom ")(\w+)|(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\s\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?=\s+set)|(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\s\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?=\s+next)/';
print_r(preg_match_all($regex, $configFile, $matched_interfaces));

输出是

代码语言:javascript
复制
Array
(
[0] => Array
(
[0] => internal1
[1] => root
[2] => 192.168.1.1 255.255.255.0
[3] => internal2
[4] => root
[5] => 192.168.20.2 255.255.255.0
[6] => 192.168.21.2 255.255.255.0
[7] => 192.168.22.2 255.255.255.0
[8] => internal3
[9] => root
[10] => 192.168.30.3 255.255.255.0
[11] => 192.168.31.3 255.255.255.0
)

[1] => Array
(
[0] => internal1
[1] => 
[2] => 
[3] => internal2
[4] => 
[5] => 
[6] => 
[7] => 
[8] => internal3
[9] => 
[10] => 
[11] => 
)

[2] => Array
(
[0] => 
[1] => root
[2] => 
[3] => 
[4] => root
[5] => 
[6] => 
[7] => 
[8] => 
[9] => root
[10] => 
[11] => 
)

[3] => Array
(
[0] => 
[1] => 
[2] => 192.168.1.1 255.255.255.0
[3] => 
[4] => 
[5] => 192.168.20.2 255.255.255.0
[6] => 
[7] => 
[8] => 
[9] => 
[10] => 192.168.30.3 255.255.255.0
[11] => 
)

[4] => Array
(
[0] => 
[1] => 
[2] => 
[3] => 
[4] => 
[5] => 
[6] => 192.168.21.2 255.255.255.0
[7] => 192.168.22.2 255.255.255.0
[8] => 
[9] => 
[10] => 
[11] => 192.168.31.3 255.255.255.0
)

)

编辑回答后续

(?<=\vedit ") --这是在构造后面的一个正面的外观,必须放在括号中。这个位?<=指定了\v后面的正向匹配垂直空格和edit "实际上是匹配编辑“,后面是(\w+),意思是尽可能多次匹配单词字符,把它放在括号中会创建一个捕获组,以便稍后引用匹配。

可以将组名称添加到捕获组中,以便将它们作为命名数组返回。

代码语言:javascript
复制
$regex = '/(?<=edit ")(?<name>\w+)\K|(?<=vdom ")(?<vdom>\w+)|(?<ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\s\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?=\s+set)|(?<secondary>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\s\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?=\s+next)\K/';
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46594027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档