'Regular expression to match string and then specific numbers
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.
Solution 1:[1]
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
Solution 2:[2]
Longhand version I came up with:
^BV001ID[0,2,9][0-9][0-9]
Solution 3:[3]
Please try this \BV001ID[0,2,9]\d\d
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | trincot |
| Solution 2 | Gregory A. Miller |
| Solution 3 | Jayanthi Chalikonda |
