0

I need to grab the 2 character letter and numbers after that:

What I want is:

AB 12 CD-12345-67   -> CD-12345
AB12 CD 12345-67 ->  CD 12345 
AB-12CD12345-6 ->  CD12345
ABC1234556 -> no match, as I look for 2 character letter and numbers after that.
ABC-1234556 -> no match, as I look for 2 character letter and numbers after that.
A1-BC-12D345-56 -> no match, after 2 characters letter, numbers must come 

I used this regex

 [A-Z]{2}[ |\-]?\d+

Which grabs CD-12345 and AB 12, in the first example. I just need CD-12345. Also it grabs BC1234556,BC-1234556, BC-12 in the last three example which i don't want. Sometimes, space,no space or - character placed between numbers and letters block.

Thank you very much.

faust
  • 3
  • 1
  • 1
    Try [`(?<![A-Z])[A-Z]{2}[ -]?[0-9]{3,6}`](https://regex101.com/r/sY8zY3/1) – Wiktor Stribiżew Jul 28 '16 at 13:11
  • Do you use it in JavaScript (you mentioned that you are using a regexr website to test the regex)? You should add the relevant tag to the question. In JS, use [`(^|[^A-Z])([A-Z]{2}[ -]?[0-9]{3,6})`](https://regex101.com/r/sY8zY3/3) and use group 2 value. – Wiktor Stribiżew Jul 28 '16 at 13:19

1 Answers1

0

based on what you posted

^.*(?<![A-Z])([A-Z]{2}[- ]?\d++)(?![A-Z])

Demo

alpha bravo
  • 7,838
  • 1
  • 19
  • 23