0

So, I'd like to check if a string of letters and digits is in correct format

Here's what it needs to look like:

123abc#456def#

I've tried something like that:

Regex r = new Regex(@"^([0-9a-zA-Z]#)([0-9a-zA-Z]#)$");

Didn't help me...

Glitch
  • 42
  • 9
  • 1
    `[0-9a-zA-Z]{6}` You need to quantify your sets. – ctwheels Dec 08 '17 at 22:44
  • You should consider clarifying the requirements - from your example I would infer it's "three digits followed by three lowercase alpha characters" etc.. but there's room for error in inference.. Is it always 3 of each? Are uppercase characters allowed? Going through the mental gymnastics to adequately describe the regex requirements may also help you to have an "aha!" as you write the regex. – Rick Riensche Dec 08 '17 at 22:54

2 Answers2

0

Brief

As I mentioned in the comments you need to quantify your sets as such: [0-9a-zA-Z]{6}

Code

^([0-9a-zA-Z]{6}#)([0-9a-zA-Z]{6}#)$

You can also do ^([0-9a-zA-Z]{6}#){2}$ since .net keeps multiple captures (not just the last one). See this shorter regex in use here. Click on Table and under the heading $1 expand 2 captures

For ensuring 3 numbers followed by 3 letters use either of the following patterns:

^(\d{3}[a-z]{3}#)(\d{3}[a-z]{3}#)$
^(\d{3}[a-z]{3}#){2}$

And add IgnoreCase option

ctwheels
  • 21,901
  • 9
  • 42
  • 77
0

I would go for the following regex:

^([A-Za-z0-9]{6}#){2}$

But you didn't specify the format of your strings, so It's actually a guess game. Are those hash-delimited groups only recurring two times? Do you want to match 3 numbers followed by 3 letters in each group?

Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98