Home > Software design >  Regular Expression for this these characters : BASIC-GT-02-0078276
Regular Expression for this these characters : BASIC-GT-02-0078276

Time:11-28

I need a regular expression for these characters. The first 5 characters "BASIC" is fixed for every string, followed by a "-", followed by next 2 characters which should be Alphabets, then 2 numbers and then 7 numbers.

I formed this one, but it not working.

^"BASIC"{5}[-]{1}[A-Z]{2}[-]{1}[0-9]{2}[-]{1}[0-9]{7}

some valid matches : BASIC-KH-67-8743532, BASIC-RF-00-2245890..

CodePudding user response:

You may use:

^BASIC-[A-Z]{2}-[0-9]{2}-[0-9]{7}$

This pattern says to match:

  • ^ from the start of the string
  • BASIC match literal string "BASIC"
  • [A-Z]{2} match 2 uppercase letters
  • - hyphen
  • [0-9]{2} match 2 digits
  • - hyphen
  • [0-9]{7} match 7 digits
  • $ end of the string
  • Related