0

I have a text field which I need to validate using a regex. My requirement is as follow:

CCCCNNNNNN or CCCCNNNNNNN (Template)

1234ABCDEFG or 123-ABCDEFG (Example string)

Rules:

  • The whole string is maximum 25 characters
  • The first four characters (CCCC) must be alphanumeric
  • CCCC is 4 characters exactly and can be digits or number
  • CCCC can have a dash sign as 4th character
  • NNNNNNNNNNNN can be up to 21 characters and only numbers

E.g. AAAA 1234 A58- is a valid string for CCCC.

Here is my research notes:

  • I will need to match numerics first
  • I will need the + char to specify to match this pattern X times
  • I will need to match letters after that for 8-9 spaces

There is a wonderful post on RegEx patterns here:

Matching numbers with regular expressions — only digits and commas

My goal is to apply this REGEX pattern to a text box Mask in a WinForms app.

Community
  • 1
  • 1
Shawn J. Molloy
  • 2,180
  • 3
  • 38
  • 56
  • I've deleted my answer - please go back and write a sensible question. – ObscureRobot Nov 18 '11 at 20:14
  • So sorry, I've clarified my requirements. Hope this provides a clearer idea of what I need. – Shawn J. Molloy Nov 18 '11 at 20:34
  • not quite enough: to me, "first four characters (CCCC) must be alphanumeric" means either `ABCD` or `1234` and the requirement "NNNNNNNNNNNN can be up to 21 characters and only numbers" conflicts with your example `1234ABCDEFG`. then, what does `I will need to match letters after that for 8-9 spaces` mean, exactly? – Code Jockey Nov 18 '11 at 20:39
  • try to state what is allowed - and if necessary, what is not allowed - in a form like the comments in my answer – Code Jockey Nov 18 '11 at 20:40

1 Answers1

3

....

....

...yeah - I think the answer you are looking for (and I stress "think") is this expression:

^[0-9A-Za-z]{3}[0-9A-Za-z-]\d{0,21}$

thats:

^               # assert beginning (not in the middle)
[0-9A-Za-z]{3}  # three characters that are: 0-9 or a-z (upper or lower)
[0-9A-Za-z-]    # one character that is: 0-9 or a-z (upper or lower) or a dash
\d{0,21}        # anywhere from 0 to 21 digits
$               # assert at the end (not somewhere in the middle

If you want to match several cases of this expression, put the above expression (minus the assertions) into parantheses (()) along with whatever is allowed to separate these values - I chose \s or "whitespace") and then use the + quantifier:

^([0-9A-Za-z]{3}[0-9A-Za-z-]\d{0,21}\s+)+$

will match/validate the following input:

1234567890 AAAA123456789012345678901 GGG-123       hhh5   A1B2000000000

If you wanted something else, you'll have to ask a clearer question (there's a lot of contradiction and repetition in your question that makes it EXTREMELY confusing)

Code Jockey
  • 6,348
  • 6
  • 27
  • 43
  • last I checked, a standard MaskedTextBox does not use regular expressions – Code Jockey Nov 18 '11 at 20:36
  • Thank you for taking a shot at answering this despite the contradictions Code Jockey; I've cleaned up my question to provide straight forward requirements for my expression validation rules. – Shawn J. Molloy Nov 18 '11 at 20:37
  • The control I'm using is a 3rd party control which allows for MaskTypes and RegEx masks – Shawn J. Molloy Nov 18 '11 at 20:38
  • @Shawn How far off is the expression (at least based upon my rephrasing of the requirements) - what is wrong or needs to be changed/tweaked a little or added or removed? – Code Jockey Nov 18 '11 at 20:47
  • 1
    Thank you @CodeJockey this seems to be what I needed to get started. Any adjustments to the regex I will research and implement myself. Thanks again; your help is very much appreciated. – Shawn J. Molloy Nov 18 '11 at 21:35