我坚持这个问题,并希望那里的任何人都可以帮助我。
我有一个包含以下行的配置文件:
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和secondary-ip(s)以及以下正则表达式:
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);
第一个.+
是所有匹配的而不是其他的!
任何建议的答案
答案 0 :(得分:0)
我喜欢正则表达式
$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));
输出
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 "
字面上匹配编辑&#34;。接下来是(\w+)
,这意味着尽可能多地匹配单词字符,将其放在括号中会创建一个捕获组,以便稍后引用该匹配。背后的正面观察意味着(\w+)
模式只有在它前面的序列背后的外观也匹配时才会匹配
您可以将组名添加到捕获组,以将它们作为命名数组返回
$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/';