-2

I know there are similar question asked (might the same question here) but I can't seems to get my regex to work. I tried the answer there but didn't work which is below.

^[A-Z]{2}\d+

I'm trying to check if the country serial or organisation numbers start with specific two capital letters. For example BE0462427110 is from a Belgium company so all the org. numbers should start with BE. German once have DE and Italian once have IT in front.

So I tried below my own version as well.

^([BE]{2})([\d]{6,10})$

Below is a test block I use to extract the numbers from using regex. And the number is after "BEPC0 PARTS".

C0MMUNAUTE EUR0PEENNE 1 ExpCd1teur/Exp0rtateur (2) N0. BEPC0 PARTS BE0462427110 Rue Chaum0nt(Herm) 4 D F BE 4480 Eng1s E D q 1jj - Dest1na1re (8) N0, N M BUCHER 1ANDTECHN1K SA 7

After the above regex didn't work I tried another.

^([B][E])([\d]{6,10})$

So what am I doing wrong here ?

S4NDM4N
  • 904
  • 2
  • 11
  • 26

1 Answers1

1

This anchors at the beginning and end of the string:

^([A-Z]{2})([\d]{6,10})$

You are looking for a pattern somewhere within a string, so you can anchor by word boundaries \b:

\b([A-Z]{2})([\d]{6,10})\b
Peter Thoeny
  • 7,379
  • 1
  • 10
  • 20