What is wrong with this regex (VBScript / JavaScript flavor)?

I am trying to run a regular expression in VBA code that uses Microsoft VBScript Regular Expressions 5.5 (should match the 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 that interest me give a good match 123456, ignoring the last digit. Fine. (The check digit is fixed, but this is not a serious problem for me.)

But when the BOTH optional parts go away (they are optional), the match captures the last digit.

OK:

Input: 123123456 Match:123456

No alpha, no check digit. Good coincidence.

OK

Input: 123A1234567
Match:123456

, , , - 123456.

: X1231234567
: 123456

, , , - 123456.

BAD

: 1231234567
: 234567

, , 234567.

http://www.regular-expressions.info/javascriptexample.html http://www.regular-expressions.info/vbscriptexample.html.

? , ? , .

: ( ). .

UPDATE: , (!):

: ^[A-Z]?[0-9]{3}[A-Z]?([0-9]{6})-?([0-9])?$

+3
3

, 1 [0-9A-Z]? , - , .

( ), ?, .

[A-Z]?, - .

+2

, - ? $

+1

. , , . :

([0-9A-Z]{6})\d$

I'm also not sure if you need ...?. None of your input points to this. (but you can add it)

Also a faster way to do this would be the VB6 equivalent for substr if the input always has the same length.

0
source

Source: https://habr.com/ru/post/1746421/


All Articles