Regex that matches multiple new lines until finding patern

I am not very familiar to regex and I am having trouble to create a regex that solves my problem.

I want to create a regex that finds the following example: (What the regex should match is in bold)

Action type: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam sodales tincidunt ipsum ut ullamcorper
Phasellus rhoncus quam id eros volutpat, ac sodales magna tincidunt Phasellus rhoncus quam id eros volutpat, ac sodales magna
Phasellus rhoncus quam id eros volutpat, ac sodales magna tincidunt

Number Name Degree
11111111 LOREM IPSUM COMPUTER ENGINEERING
31837183 DOLOR IPSUM COMPUTER ENGINEERING

Total: 2

Action type: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Number Name Degree
128172818211 SIT AMET IPSUM COMPUTER ENGINEERING
12183781 CONSECTETUR ELIT COMPUTER ENGINEERING
128172818212 ETIAM SODALES COMPUTER ENGINEERING
128172818213 IPSUM UT COMPUTER ENGINEERING
128172818215 SODALES MAGNA COMPUTER ENGINEERING

Total: 5

What I have accomplished so far, is generating a regex that matches the lines with success and the first line of the action type, but not the subsequent. I would like to match everything that comes after action type till the line that contains Number, Name and Degree.

The currently regex I am using is (Action type: .+?\n|[0-9]{8,12} .+?\n). A preview of the current executiong using regex101.com is attached.

Regex 101 image of my example

As You can see, it works well for the second example, but it does not fulfil my needs with regard to the first one.

Is it possible to adapt my current regex to fit these multilines?

>Solution :

Try:

^Action type:.*?(?=^Number Name Degree)|^\d{8,12}[^\n]+

Regex demo.


^Action type:.*?(?=^Number Name Degree) – this matches all text beginning with Action type: until ^Number Name Degree is found.

^\d{8,12}[^\n]+ – this matches all lines beginning with 8-12 digits.

Note: the expression needs (?s) modifier

Leave a Reply