php preg_match_all 多行模式
php preg_match_all of multiple lines pattern
我被这个问题困住了,希望有人能帮助我。
我有一个包含以下行的配置文件:
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);
第一个 .+
是所有匹配的,而不是其他的!
感谢任何建议
我喜欢正则表达式
$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+)
,这意味着匹配单词字符的次数与你可以,把它放在括号中创建一个捕获组,这样你可以稍后引用匹配。正向后看意味着 (\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/';
我被这个问题困住了,希望有人能帮助我。
我有一个包含以下行的配置文件:
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);
第一个 .+
是所有匹配的,而不是其他的!
感谢任何建议
我喜欢正则表达式
$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+)
,这意味着匹配单词字符的次数与你可以,把它放在括号中创建一个捕获组,这样你可以稍后引用匹配。正向后看意味着 (\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/';