1

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
  • [a-zA-Z]{2} [0-9]*-[0-9]{2} would match those Commented Mar 22, 2016 at 4:57
  • 2
    if you're asking how to match a space, you just use a space. Commented Mar 22, 2016 at 4:57

2 Answers 2

1

\s is indicates space use following expression it will works
[a-zA-Z0-9\s]{5,25}

Sign up to request clarification or add additional context in comments.

Comments

0

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

\s+ matches more than just a space. it matches any amount of whitespace
@juharr of course, it was just a demonstration, but I will update the answer to address this.
Thanks. This works well. How about if string contains any number of digits after the space like "GB 12-23232-23232"
Just tried your given regex. It also works for my above scenario. Thanks
@OseeAliz if you mean number-number-number... then you can use s.t like this: [A-Z]{2} +(\d+-)+\d+

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.