Reputation: 1
I need to validate the following string
case 1: 123E
The first three are numeric and then the alphabet.
I have created the following pattern
var pattern = @"^[0-9]{2}[a-zA-Z]{1}"
Is this correct?
Upvotes: 0
Views: 50
Reputation: 76000
You were close, as per my comment you can use:
^\d{3}[a-zA-Z]$
See the online demo
^
- Start string anchor.\d{3}
- Three digits where \d
is shorthand for [0-9]
1[a-zA-Z]
- A single alpha char.$
- End string anchor.See a working .Net
example here
1: This would only be the case if the ECMAScript
flag is enabled. Otherwise \d
is matching non-ascii digits as per this link. Thanks @WiktorStribiżew for noticing.
Upvotes: 1
Reputation: 65
Something like that should work: ^[0-9]{3}[a-zA-Z]$
You put {2}
for numbers but in your case there are 3 of them so I wrote {3}
instead.
You don't need to write {1}
because [a-zA-Z]
is enough to find exacly one letter.
I also added endline tag $
to ensure that there are no more symbols after letter.
Upvotes: 1