Reputation: 897
I'm a bit stuck on trying to figure out how to get lookaround to work for me in regards to an if-else type situation. When I try to implement the if-else idiom on this site I end up with an error. Here's what I have:
MCV(1|0)(\d)(?<!1)\1[0-6]|[0-9]
And this is an example of what I'm trying to match
These
MVC123 MVC034 MVC001 MVC196
Not these
MCV197 MCV000 MCV876
Thus matching where MCV
appears and the number component ranges from 1-196
Upvotes: 1
Views: 59
Reputation: 36110
Here is your regex broken down:
MCV(1|0)(\d)(?<!1)\1[0-6]|[0-9]
MCV
- match MCV literally(1|0)
- match 1
or 0
(\d)
- match a digit(?<!1)
- make sure that last digit wasn't a 1
\1
- match exactly what the first group matched (aka if (1|0)
matched 1
- match 1
, otherwise - 0
)[0-6]
- match a digit from 0
to 6
|[0-9]
- alternatively, match all of the above or just a digit from 0
to 9
As to what a real solution would be:
MVC(00[1-9]|0[1-9]\d|1[0-8]\d|19[0-6])
The 1-196 part:
00[1-9]
- if we start with 00
, the third digit shouldn't be a 0
, aka 1 to 90[1-9]\d
- if we start with 0
and a second digit, which isn't a 0
then any digit is acceptable for third position, aka 10 to 991[0-8]\d
- if we start with 1
and a second digit, which isn't 9
then any digit is acceptable for third position, aka 100 to 18919[0-6]
- if we start with 19
then only digits from 0
to 6
are acceptable for third position, aka 190 to 196Upvotes: 1
Reputation: 785761
You can use this regex:
\bMVC(0*[1-9][0-9]?|1[0-8][0-9]|19[0-6])\b
Upvotes: 1