1

I apologize if this is a repeat question, as I know there are many about Regex on StackOverflow, but I have yet to find an answer or a level of help I need.

I have a string that needs to be a length of 8 where:

  • The first two characters are letters

  • The next five characters are numbers

  • The last character is a letter

For example: "AB12345C"

I have been using the examples from MSDN & DotNetPerls to try and understand how to use arguments properly, but after a couple days of reading around I still can't get it to pass.

I am currently trying to use:

public Tuple<bool, string> stringFormatCheck(string input)
{            
     if (Regex.IsMatch(input, @"^[a-zA-Z]\d{2}[0-9]\d{5}[a-zA-Z]$") == true)
        return Tuple.Create(true, "String is Fine");
     else
        return Tuple.Create(false, "String Format is incorrect");
}

Can someone show me how to use this argument properly or somewhere I can get a better understanding of the Regex Class? Thank you.

EDIT1: The second Z in my first argument is now capitalized.

8
  • Tools like "expresso" (ultrapico.com/expresso.htm) or "the regulator" will help you analyze, build and test your regular expression, have you tried one of those? Commented Apr 21, 2016 at 15:30
  • @PaoloTedesco No, I had no idea these tools existed. I will definitely look into those, thank you! Commented Apr 21, 2016 at 15:30
  • Your first [a-zA-z] is wrong. Second Z should be uppercase. Curious how you're testing your regex and what convinces you it's not working. Commented Apr 21, 2016 at 15:31
  • @adv12 From my understanding, Regex.IsMatch will return true if the input string matches the Regex arguments. When I pass in a string that I assign myself, which I know fit the pattern I am trying to match, it has only returned false this far. Commented Apr 21, 2016 at 15:33
  • @Rinktacular: put your regex in Expresso -> regular expression panel: the "regex analyzer" panel will show you immediately what's wrong :) Commented Apr 21, 2016 at 15:35

2 Answers 2

1

The right pattern is

"^[A-Za-z]{2}[0-9]{5}[A-Za-z]$"

with, IMHO, clear interpretation:

^           - string start (anchor)
[A-Za-z]{2} - 2 letters A..Z or a..z
[0-9]{5}    - 5 digits 0..9
[A-Za-z]    - letter A..Z or a..z
$           - string end (anchor)

And so the implementation can be

public Tuple<bool, string> stringFormatCheck(string input)
{            
    if (Regex.IsMatch(input, @"^[A-Za-z]{2}[0-9]{5}[A-Za-z]$"))
        return Tuple.Create(true, "String is Fine");
    else
        return Tuple.Create(false, "String Format is incorrect");
}

Please, notice, that [0-9] is a better choice than \d since you, probably, don't want let, say, Persian digits like "AB۰۱۲۳۴C" ;

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

5 Comments

Thank you very much! Just out of curiosity, are the capital A-Z required to go before the lower case a-z in the first and last arguments?
@Rinktacular: no, the order can be arbitrary A-Za-z as good as a-zA-Z
Fantastic, the code passes as expected now. Thank you very much, I can see where I was wrong now and can properly use this in my future projects!
You're welcome! In your current pattern, just drop both \d
I kept seeing /d in almost every example I saw online so I was led to believe that was important to the argument I was making ha. Your example is very clear and very helpful, thanks again!
1

Try this: ^[a-zA-Z]{2}[0-9]{5}[a-zA-Z]$

Your regex: ^[a-zA-z]\d{2}[0-9]\d{5}[a-zA-Z]$ doesn't work for multiple reasons. First, the second z should be capitalized. Then, the first \d is trying to match a digit, so you're saying "Match any letter then two digits." You make the same mistake with the second \d: you say "Match any digit ([0-9]) and then match 5 digits (\d{5}).

4 Comments

(Possible) counter example: "AB۰۱۲۳۴C" is accepted, when I'm sure it should be rejected; please notice Persian digits.
@DmitryBychenko: That can be helped with a RegexOptions.ECMAScript.
@DmitryBychenko, thanks. I've replaced the \d with [0-9].
Thank you, I got my answer from the other one on this question, but both of you have given me a much better understanding of Regex arguments for my use of them.

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.