Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match TR3456789, ME3456789 or 123456789

Tags:

regex

I am trying to create a regex that will match any of the following values:

  • TR3456789
  • ME3456789
  • 123456789

The rule is: Any string where the first two characters are either "ME", "TR" or 2 numbers, and the last 7 characters are numbers.

(\bME\b|\bTR\b|[0-9]{2})[0-9]{7}

I can't get my regex to match on all the options. Any regex gurus out there who can help me out?

like image 259
Amanda Kitson Avatar asked Jan 05 '23 23:01

Amanda Kitson


2 Answers

The regex would be: (ME|TR|[0-9]{2})[0-9]{7}

like image 190
Martin Cup Avatar answered Jan 07 '23 14:01

Martin Cup


This is my example: /^(ME|TR|\d{2})\d{7}$/, and here you can try it.

like image 40
cn007b Avatar answered Jan 07 '23 12:01

cn007b