Lukasz Flak
Lukasz Flak

Reputation: 33

Regular Expression : Multiline check problem

Hello i have problem with this regexp

!
interface TenGigabitEthernet 1/49
 description Uplink
 no ip address
 switchport
 no shutdown
!
interface TenGigabitEthernet 1/50
 no ip address
 shutdown
!
interface TenGigabitEthernet 1/51
 no ip address
 shutdown
!

i tried this regexp (interface) ((.\s.)+) but it is not working becuse it match "interface" and the rest of text

I need to catch in first group "interface" and in the second i need all until first occur of "!" so for example: first group:

interface

second group:

TenGigabitEthernet 1/51
 no ip address
 shutdown

How i can do this?

Upvotes: 3

Views: 57

Answers (3)

The fourth bird
The fourth bird

Reputation: 163457

If the content itself can contain a !, you could check for a ! at the start of the line and repeat matching all lines until you encounter a ! at the start.

^(interface)\s*(.*(?:\n(?!!).*)*)

In Java

String regex = "^(interface)\\s*(.*(?:\\n(?!!).*)*)";

Regex demo

Upvotes: 0

user11116003
user11116003

Reputation:

Try this:

(interface)\s+([^!]+)

Here Is Demo

Upvotes: 3

CinCout
CinCout

Reputation: 9619

Use this:

(interface)\s*([^!]+) /g

The first group captures the hard-coded interface. The second group captures everything other than !, by skipping the leading whitespaces, if any. The global flag /g ensures all matches.

Demo

Upvotes: 1

Related Questions