I'm trying to run a regular expression in VBA code that uses Microsoft VBScript Regular Expressions 5.5 (should be the same as JavaScript regular expression).
Regular expression: ^[0-9A-Z]?[0-9A-Z]{3}[A-Z]?([0-9A-Z]{6})-?([0-9])?$
Input: X123A1234567
Match: 123456
The six characters I'm interested in give a good match of 123456
, ignoring the last (check) digit. Perfect. (The check digit is captured, but it's not a major concern to me.)
But when BOTH the optional portions are gone (they are optional) the match grabs the last digit.
GOOD:
Input: 123123456
Match: 123456
No alphas, no check digit. Good match.
GOOD
Input: 123A1234567
Match: 123456
Leave in the optional middle alpha, take out the optional leading alpha, leave in check digit, and we still get the good match of 123456
.
GOOD
Input: X1231234567
Match: 123456
Leave in the optional leading alpha, take out the middle optional alpha, leave in check digit, and we still get a good match of 123456
.
BAD
Input: 1231234567
Match: 234567
Take out BOTH optional alphas, leave in check digit, and we get a bad match of 234567
.
Have a look at the regular expression testers on http://www.regular-expressions.info/javascriptexample.html or http://www.regular-expressions.info/vbscriptexample.html.
What am I missing, here? How can I get the regular expression to ignore the last digit when both optional alphas are missing? The regular expression is used to feed a lookup system, so that no matter what format the input data, we can match to a complete value.
UPDATE: None of the above examples includes the hyphen (shown in regex). Input data with the hyphen and check digit has always matched.
UPDATE: working regex, thanks to the below suggestions (thanks!):
Regular expression: ^[A-Z]?[0-9]{3}[A-Z]?([0-9]{6})-?([0-9])?$