Home > Mobile >  Regular expression to match string and then specific numbers
Regular expression to match string and then specific numbers

Time:05-06

I am trying to create a regular expression for a string entry with two components:

BV001ID is going to be the first part of the string, and it must always begin with this.

Then there will be a three digit number. The first digit must be either 0, 2, or 9. The following two digits can be any number.

So for example, a valid response can be BV001ID041. A valid response could not be BV001ID301 or BV001010.

This seems to be working for the three digit number: [0,2,9]\d\d

But I'm not sure how to add the string in front.

CodePudding user response:

You can just prefix literal characters into your regular expression. You should however remove the commas from the character class, as otherwise you actually allow a comma. Every single character in a character class represents a possible match, so only list those, like [029]

So do:

BV001ID[029]\d\d

CodePudding user response:

Please try this \BV001ID[0,2,9]\d\d

CodePudding user response:

Longhand version I came up with:

^BV001ID[0,2,9][0-9][0-9]
  • Related