Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx - Match a number that has different beginnings

Tags:

regex

I'm new to RegEx and I'm trying to match a specific number that has 8 digits, and has 3 start options:

  • 00
  • 15620450000
  • VS

For Example:

  • 1562045000012345678
  • VS12345678
  • 0012345678
  • 12345678

I don't want to match the 4th option. Right now I have managed to match the first and third options, but I'm having problems with the second one, I wrote this expression, trying to match the 8 digits under 'Project':

156204500|VS|00(?<Project>\d{8})

What should I do?

Thanks

like image 667
Eyal Avatar asked Dec 23 '22 15:12

Eyal


1 Answers

With your shown samples, please try following regex once.

^(?:00|15620450000|VS)(\d{8})$

OR to match it with Project try:

^(?:00|15620450000|VS)(?<Project>\d{8})$

Online demo for above regex

Explanation: Adding detailed explanation for above.

^(?:00|15620450000|VS)  ##Checking value from starting and in a non-capturing group matching 00/15620450000/VS here as per question.
(?<Project>\d{8}        ##Creating group named Project which is making sure value has only 8 digits till end of value.
)$                      ##Closing capturing group here.
like image 98
RavinderSingh13 Avatar answered Jan 04 '23 00:01

RavinderSingh13