I have a text field which I need to validate using a regex. My requirement is as follow:
CCCCNNNNNNorCCCCNNNNNNN(Template)
1234ABCDEFGor123-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.
….
….
…yeah – I think the answer you are looking for (and I stress “think“) is this expression:
thats:
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\sor “whitespace”) and then use the+quantifier:will match/validate the following input:
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)