I'm trying to build a regex in C# that has the following characteristics.
I've tried
\d?ABC
but still matches things like ZABC
, ABCD
, 2ZABC
.
any pointers?
You need anchors to represent the start and end of the string:
^\d?ABC$
Also, ?
means 0 or 1. 0 or more is *
:
^\d*ABC$
Also note that depending on the active Culture
in .NET \d
can be interpreted as "any Unicode digit character". If you really only want the ASCII digits use a character class:
^[0-9]*ABC$
The tutorial on that website is a great resource to learn regular expressions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With