我有一个配置文件:
config line 1
....
config line n
router mk1
ip 10.1.1.2
deviceType t1
sub config line!
sub config line 2
!more sub config
!
!!!
more config lines
router mk2
ip 10.1.1.2
sub config line1
sub config line 2
deviceType t2
!
每个路由器块以新行上的路由器一词开头,以!结尾。在新的一行上。一个配置文件可以包含许多路由器块,每个子块都以单个空格开头。子块中的行可以以任何顺序排列。我想选择一个包含特定行的块,例如deviceType t2。
到目前为止,我可以使用以下命令识别所有路由器块:
(?ms)^router mk.*?^!$
但我只需要一个包含deviceType t2行的块
下面的正则表达式应为最重要的正则表达式引擎的工作(尽管它可能需要更换\R
同\r?\n
)。我使用PCRE(PHP)正则表达式引擎对其进行了测试。
(?m)^router .+\R(?: .*\R)* deviceType t2\R(?: .*\R)*!$
引擎执行以下操作。
(?m) : multiline mode (^ and $ match beginning and end of line)
^ : match start of line
router .*\R : match line beginning 'router ', including terminator
(?: .*\R)* : match a line including terminator that begins with a
space in a non-capture group, execute 0+ times
deviceType t2\R : match line 'deviceType t2', including terminator
(?: .*\R)* : match a line including terminator that begins with a
space in a non-capture group, execute 0+ times
!$ : match the line '!'
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句