kakoni
kakoni

Reputation: 5140

Regex, matching multiple lines

Given that I have following text;

123            XXXXX                                         123-456-678
               YYYYYY
               121-121-1121-11 Foo
               Street 11

               12121 FINLAND
               Building
               Lorem Ipsun
               Ipsum


124            XXXXX                                         123-456-890
               YYYYYY
               121-121-1121-21 Bar
               Street 12

               12121 SWEDEN
               House
               Lorem Ipsun2
               Ipsum2

How would I capture this into two matches? Meaning first one would start with 123 line and including all the lines until we encounter 124 line, which would be another match.

Most success I've had with (?:^\d+)(\s+.*)+ but thats way too greedy.

Upvotes: 0

Views: 76

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174836

You could try this regex also,

(?<=\n|^)((?:\d+.*?\n)(?=\d+)|(?:\d+.*?)(?=$))

DEMO

Upvotes: 1

anubhava
anubhava

Reputation: 786031

You can use this regex:

/^(\d+.*?)(?=^\d+|\z)/gms

RegEx Demo

Upvotes: 2

Related Questions