I have a string value like "GX 123-02" or "ML 35-02". The numbers entered after the letters can be any long, but there must be a space after the alphabet followed by the number values. How can I create a regex expression in C# to check if the value is entered in this format?
2 Answers
So if I got you right, you want exactly two letters followed by at least a space and number-number, you can use this regex:
[a-zA-Z]{2}\s+\d+-\d+
Note that \s matches any white space if you want exactly the space character to be matched at least one time you can simply just replace \s with a space like this:
[a-zA-Z]{2} +\d+-\d+
5 Comments
juharr
\s+ matches more than just a space. it matches any amount of whitespace
Arin Ghazarian
@juharr of course, it was just a demonstration, but I will update the answer to address this.
OseeAliz
Thanks. This works well. How about if string contains any number of digits after the space like "GB 12-23232-23232"
OseeAliz
Just tried your given regex. It also works for my above scenario. Thanks
Arin Ghazarian
@OseeAliz if you mean number-number-number... then you can use s.t like this:
[A-Z]{2} +(\d+-)+\d+