繁体   English   中英

在结束行之前有多行的正则表达式

[英]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,})

等等(我忘记了其他所有方式)你们有什么推荐的人吗?

\binterface\b[\s\S]*?!

这应该为你做。看演示。

https://regex101.com/r/vV1wW6/45

为了在两个字符串之间匹配一些包含换行符的文本,您需要将惰性点匹配技术与 DOTALL(或在其他术语中,单行)修饰符一起使用: .*? .

当使用 PCRE 正则表达式模式(SublimeText 使用这种正则表达式风格)时,您可以使用此修饰符的内联版本: (?s)

惰性点匹配确保开始和最左端边界之间的匹配。 1 q将在1 q 4q1.*?q模式匹配。 贪婪匹配1.*q将匹配整个字符串。

因此,在您的情况下,您可以使用以下正则表达式:

(?s)interface.*?!
|1 |   2     |3|4

这里,

  1. 内联 dotall 修饰符
  2. 起始边界,文字interface字符串
  3. 懒点加工构造
  4. 字面意思! 作为结束边界

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM