[英]Using RegEx to Extract Multiple Lines Using Alternate End of Line Characters (newline, return, end of string)
[英]Regex with multiple lines before end line
大家好,我有一个问题,我有一个这样的字符串:
interface GigabitEthernet0/3/0/0
description PhysicalInterface
!
interface GigabitEthernet0/3/0/0.100
description Vlan100
dot1q vlan 100
!
multicast-routing
address-family ipv4
interface TenGigE0/2/0/0.3880
! Disable this interface under multicast-routing section:
disable
!
router static
address-family ipv4 unicast
GigabitEthernet0/3/1/4.3999 192.168.100.105
所以我想使用类似 select 之间的所有内容:interface 和 ! 喜欢:
interface GigabitEthernet0/3/0/0
description PhysicalInterface
!
interface GigabitEthernet0/3/0/0.100
description Vlan100
dot1q vlan 100
!
interface TenGigE0/2/0/0.3880
!
我尝试了很多不同的方法:
interface(.*?)\n
(interface(.*?)|\n{2,})
等等(我忘记了其他所有方式)你们有什么推荐的人吗?
为了在两个字符串之间匹配一些包含换行符的文本,您需要将惰性点匹配技术与 DOTALL(或在其他术语中,单行)修饰符一起使用: .*?
.
当使用 PCRE 正则表达式模式(SublimeText 使用这种正则表达式风格)时,您可以使用此修饰符的内联版本: (?s)
。
惰性点匹配确保开始和最左端边界之间的匹配。 1 q
将在1 q 4q
与1.*?q
模式匹配。 贪婪匹配1.*q
将匹配整个字符串。
因此,在您的情况下,您可以使用以下正则表达式:
(?s)interface.*?!
|1 | 2 |3|4
这里,
interface
字符串!
作为结束边界
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.