question

IanBrooke-1859 avatar image
0 Votes"
IanBrooke-1859 asked TimonYang-MSFT commented

Simple expression

I am convinced that this requirement is so simple it's embarrassing to ask but for some reason it is escaping me, perhaps I don't know enough about Regular Expressions. :)
All I need to do is validate a string which needs to match 1-4 numbers followed optionally by a single letter (upper or lower case a-z). so 9, 8a, 225z are all valid but, a, 2ab, 12345a, are invalid.
All help very welcome!

ps I have no idea what I'm wanted to enter under "Tags" so I just picked one at random!

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered TimonYang-MSFT commented

Please try if this can meet your needs:

             Regex regex = new Regex(@"(?<=^[0-9]{1,3}(?:a|A|z|Z|$)\b)");
             string test = "123a";
             bool re = regex.IsMatch(test);

If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@IanBrooke-1859
Is that regex pattern useful to you?
If it doesn't suit you or you still have any questions, please feel free to let us know.

0 Votes 0 ·
Viorel-1 avatar image
0 Votes"
Viorel-1 answered

Try this simple expression too:

^[0-9]{1,4}[a-zA-Z]?$

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.