virusivv
virusivv

Reputation: 347

Regex with multiple lines before end line

Hello guys i have a question, i have a string like this:

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

so i would like to use something like select everything between: interface and ! like:

interface GigabitEthernet0/3/0/0
     description PhysicalInterface
    !

interface GigabitEthernet0/3/0/0.100
     description Vlan100
     dot1q vlan 100
    !


interface TenGigE0/2/0/0.3880
       ! 

i have tried many different ways:

interface(.*?)\n
(interface(.*?)|\n{2,})

etc(i have forgotten every other ways) what do you recommend guys?

Upvotes: 2

Views: 48

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626699

In order to match some text including newlines between two strings, you need to use a lazy dot matching technique together with a DOTALL (or in other terminology, singleline) modifier: .*?.

When using PCRE regex patterns (SublimeText uses this regex flavor), you can use an inline version of this modifier: (?s).

Lazy dot matching ensures a match between the start and leftmost end boundary. 1 q will be matched in 1 q 4q with the 1.*?q pattern. Greedy matching, 1.*q, would match that whole string.

So, in your case, you can use the following regex:

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

Here,

  1. The inline dotall modifier
  2. Starting boundary, a literal interface string
  3. Lazy dot maching construct
  4. Literal ! as an end boundary

Upvotes: 1

vks
vks

Reputation: 67968

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

This should do it for you.See demo.

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

Upvotes: 1

Related Questions