Rubi
Rubi

Reputation: 43

Regex multiple lines and capture specific text between

I have the following string (that changes except the first row, thats my starting point):

Action 0100: Register: 
2021.01.05 14:38:08 CET 
REF: 12345678910
FAK: Street,ZIP

===========================

I want to capture everything between the date row until the row with the equal signs.

REF: 12345678910
FAK: Street,ZIP

I tried something like this at first when i only had one row, but when i have both REF and FAK rows, i need something better:

(?:.+Register.+\n.+\n)(.+)

I would like to have everything captured into one group. Because i need to verify if there is any other rows then REF or FAK

Upvotes: 1

Views: 106

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626691

You can use

Register(?:[^\r\n]*\r?\n){2}([^\r\n]+(?:\r?\n[^\r\n]+)*)

Details:

  • Register - a fixed string
  • (?:[^\r\n]*\r?\n){2} - two sequences of zero or more chars other than CR and LF and then an optional CR followed with one LF char
  • ([^\r\n]+(?:\r?\n[^\r\n]+)*) - Group 1:
    • [^\r\n]+ - one or more chars other than CRLF
    • (?:\r?\n[^\r\n]+)* - zero or more repetitions of CLRF or LF line ending sequence and then one or more chars other than CR and LF.

Upvotes: 1

Related Questions