我被困在这个问题上,希望任何一个人都能帮我。
我有一个配置文件,其中包含以下行:
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:
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);对于第一个.+,一切都是匹配的,而不是其他的!
如有任何建议
发布于 2017-10-05 21:23:42
我喜欢regex
$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 "实际上是匹配编辑“,后面是(\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/';https://stackoverflow.com/questions/46594027
复制相似问题