Reputation: 19
I am trying to create a regex, but I am not sure at this point, if it is possible:
As my example: https://regex101.com/r/JRBYyw/6
Requirements:
[A-Z]{4}\s?\d{7}
which can be after 12 digit number random-amount of times (between them again, random other characters in multiple lines)[A-Z]{4}\s?\d{7}
so many times, until there is another 12 digit number somewhere in the text before it - not only directly before it, but between other text again.Upvotes: 1
Views: 1581
Reputation: 626748
You can use a two-pass approach: 1) extract all blocks of texts that start with 12-digit numbers and span up to the next 12-digit number or end of text, and 2) then extract the letter-digit patterns from each block.
Here is the first regex:
^(3\d{11})\R+([\s\S]*?)(?=\R3\d{11}|\z)
See the regex demo. The 12-digit numbers are in Group 1 here. Then, take Group 2 as input for
\d{12}|[a-zA-Z]{4}\s?\d{7}
that matches either 12 digits, or four letters, an optional whitespace and then seven digits.
See this regex demo
Upvotes: 1