Jane9256
Jane9256

Reputation: 19

Regex to match {12} digit number & other pattern, multiline, when any characters can be between them

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:

Upvotes: 1

Views: 1581

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

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

Related Questions