Reputation: 2738
I want Regular expression for such inputs.
1
1a
1b
1c
1d
2
2a
2b
2c
But if i write following inputs then it should not allow.
a
b
c
The string must start with 1 or 2 (only once and mandatory) and then followed by any character between a to z(only once)
So total string length is 2 only
the first letter will be always 1 or 2 (first letter is mandatory)
second letter will be a to z (not mandatory)
I tried this [1-2]?[a-zA-Z]?
but it allow me to enter string starts with any character..
I want this RegEx for C#.Net
Upvotes: 2
Views: 21338
Reputation: 498924
You need to anchor the regular expression - you need to specify that they need to be at the start of the string.
You also need to specify that 1 or 2 has to be there. There are several ways to do so, I chose alternation (1|2)
, thought the character class is another option ([12]
).
You do that by starting the regex with ^
:
^(1|2)[a-zA-Z]?
So, the above will match
Upvotes: 7