2

Can someone please validate this for me (newbie of regex match cons).
Rather than asking the question, I am writing this:

Regex rgx = new Regex (@"^{3}[a-zA-Z0-9](\d{5})|{3}[a-zA-Z0-9](\d{9})$"

Can someone telll me if it's OK...

The accounts I am trying to match are either of:

1. BAA89345  (8 chars)
2. 12345678  (8 chars)
3. 123456789112 (12 chars)

Thanks in advance.

5
  • From your examples is not very clear for me which are the rules. Could you explicity explain cases where your regex has to match? Commented Oct 4, 2012 at 19:10
  • A great tool for testing regex is regex fiddle: refiddle.com Commented Oct 4, 2012 at 19:14
  • I'll put my vote in for RadSoft Regular Expression Designer. radsoftware.com.au Commented Oct 4, 2012 at 19:16
  • if it's irrelivant whether your first example starts with three letters or whether the accounts are just 8 or 12 characters it changes the regex Commented Oct 4, 2012 at 19:16
  • I mean with C# if you just want to make sure the length of the account is 8 or 12 chars... you could just test the string.length == 8 || string.length == 12 and be done Commented Oct 4, 2012 at 19:19

5 Answers 5

1

You can use a Regex tester. Plenty of free ones online. My Regex Tester is my current favorite.

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

Comments

1

Is the value with 3 characters then followed by digits always starting with three... can it start with less than or more than three. What are these mins and max chars prior to the digits if they can be.

Comments

1

You need to place your quantifiers after the characters they are supposed to quantify. Also, character classes need to be wrapped in square brackets. This should work:

@"^(?:[a-zA-Z0-9]{3}|\d{3}\d{4})\d{5}$"

There are several good, automated regex testers out there. You may want to check out regexpal.

Comments

1

Although that may be a perfectly valid match, I would suggest rewriting it as:

^([a-zA-Z]{3}\d{5}|\d{8}|\d{12})$

which requires the string to match one of:

  • [a-zA-Z]{3}\d{5} three alpha and five numbers
  • \d{8} 8 digits or
  • \d{12} twelve digits.

Makes it easier to read, too...

Comments

0

I'm not 100% on your objective, but there are a few problems I can see right off the bat.

  1. When you list the acceptable characters to match, like with a-zA-Z0-9, you need to put it inside brackets, like [a-zA-Z0-9] Using a ^ at the beginning will negate the contained characters, e.g. `[^a-zA-Z0-9]

  2. Word characters can be matched like \w, which is equivalent to [a-zA-Z0-9_].

  3. Quantifiers need to appear at the end of the match expression. So, instead of {3}[a-zA-Z0-9], you would need to write [a-zA-Z0-9]{3} (assuming you want to match three instances of a character that matches [a-zA-Z0-9]

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.