1

I want to validate input in a C# TextBox by using regular expressions. The expected input is in this format: CCCCC-CCCCC-CCCCC-CCCCC-CCCCC-CCCCC-C

So I've got six elements of five separated characters and one separated character at the end.

Now my regex matches any character between five and 255 chars: .{5,255}

How do I need to modify it in order to match the format mentioned above?

6
  • A C can be any alphanumeric character (A-Z, a-z and 0-9) Commented Dec 22, 2012 at 15:54
  • @SeToY.. Do you want those characters to be same? Like is this allowed - ABCDS-ASDFS-23423...? Commented Dec 22, 2012 at 16:01
  • @RohitJain That's perfectly fine. Commented Dec 22, 2012 at 16:02
  • @SeToY.. Then your question is not clear about that. Please modify it. Commented Dec 22, 2012 at 16:03
  • @RohitJain I wasn't mistaken at that point. There's nothing to indicate they should be same, nor is it the usual use-case. Commented Dec 22, 2012 at 16:04

4 Answers 4

3

Update: -

If you want to match any character, then you can use: -

^(?:[a-zA-Z0-9]{5}-){6}[a-zA-Z0-9]$

Explanation: -

(?:                // Non-capturing group
    [a-zA-Z0-9]{5} // Match any character or digit of length 5
    -              // Followed by a `-`
){6}               // Match the pattern 6 times (ABCD4-) -> 6 times
[a-zA-Z0-9]        // At the end match any character or digit.

Note: - The below regex will only match pattern like you posted: -

CCCCC-CCCCC-CCCCC-CCCCC-CCCCC-CCCCC-C

You can try this regex: -

^(?:([a-zA-Z0-9])\1{4}-){6}\1$

Explanation: -

(?:                // Non-capturing group
  (                // First capture group
    [a-zA-Z0-9]    // Match any character or digit, and capture in group 1
  )
  \1{4}            // Match the same character as in group 1 - 4 times
  -                // Followed by a `-`
){6}               // Match the pattern 6 times (CCCCC-) -> 6 times
\1                 // At the end match a single character.
Sign up to request clarification or add additional context in comments.

5 Comments

Note the explanation is no longer relevant.
You can use regex101.com to generate accurate explanations for your regular expressions instead :)
@Lindrian. Isn't mine accurate?? Anyways thanks for the link.
@RohitJain: Sorry if I made it sound that way, it absolutely is. I just meant you could save yourself some time using that website. You can insert an expression and the text you want to match and it breaks it all down for you very nicely. Have a look here: regex101.com/r/iT7sN4 (Happy holidays and have an upvote for a good answer :))
@Lindrian.. Ah! No worry. No offense taken really. I knew you didn't meant that. Cheers :)
1

Untested, but I think this will work:

([A-Za-z0-9]{5}-){6}[A-Za-z0-9]

3 Comments

This results in having five dashes 1-----2-----3-----4-----5-----6-----
@JanDvorak. I inferred that from the posted example in the OP. Anyways, I have asked for clarification.
@RohitJain As per the clarification, your inference was hasty and incorrect.
1

For your example, in general replace C to the character class you want:

^(C{5}-){6}C$

^([a-z]{5}-){6}[a-z]$        # Just letter, use case insensitive modifier 

^([a-z0-9]{5}-){6}[a-z0-9]$  # Letters and digits..

Comments

0

Try this:

^(C{5}-){6}C$

The ^ and $ denote the begiining and end of the string repectively and make sure that no additional characters are entered.

Comments

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.