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 regex)
regex: ^[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 looksee @ the regex 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 regex to ignore the last digit when both optional alphas are missing? The regex 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!):
regex: ^[A-Z]?[0-9]{3}[A-Z]?([0-9]{6})-?([0-9])?$