RegEx Match between beginning and ending problem

I want to match everything between "02A1" and "03" IF its 14 characters in between those two.

My RegEx Pattern looks like this:

(02A1)[0-9A-Z]{14}(03)

My problem: It also matches this:

02A103EEFFFFF702A103

What am i doing wrong? "EEFFFFF7" is clearly not between 02A1 und 03 as before the EEFFFFF7 theres a 03 and after it theres a 02A1.

Can someone help me?

>Solution :

If the fourteen alphanumeric chars cannot contain a 03 substring, use

02A1(?:(?!03)[0-9A-Z]){14}03

If it cannot contain 02A1 either, use

02A1(?:(?!03|02A1)[0-9A-Z]){14}03

See this regex demo.

Details:

  • 02A1 – a 02A1 string
  • (?:(?!03|02A1)[0-9A-Z]){14} – fourteen occurrences of an uppercase ASCII letter or digit that does not start a 03 or 02A1 char sequence
  • 03 – a 03 string.

Leave a Reply