Regular Expression Problem

  • Thread starter Thread starter Paul [Paradise Solutions]
  • Start date Start date
P

Paul [Paradise Solutions]

Hi all

Was wondering of anyone can help me with why the following expression
works as expected in XMLSpy, but does not in VB.net:

[A-Za-z]{1,2}[1-9][0-9]?[A-Za-z]? [0-9][A-Za-z]{2}|GIR 0AA$

IN VB.net using Regex.IsMatch, all works OK appart from the fact that it
will allow more than 2 characters in the last section. In XMLSpy the
expression is evaluated correctly and shows an error when validating an
XML element. EG.:

Vb.net: AB1 1ABB does not generate an error
XMLSpy: AB1 1ABB generates value does not match facet pattern.

Can this be confirmed as a bug in VB?


Many thanks.

Paul
 
The IsMatch method is defined as follows:
"Indicates whether the regular expression finds a match in the input
string."
This means that substring matches are allowed. In your expression there is a
termination character "$", but it is a part of a "|" alternative. To make
this work the way you expected modify the regex string like this:
Was:
[A-Za-z]{1,2}[1-9][0-9]?[A-Za-z]? [0-9][A-Za-z]{2}|GIR 0AA$
Should be
^[A-Za-z]{1,2}[1-9][0-9]?[A-Za-z]? [0-9][A-Za-z]{2}$|GIR 0AA$

As an alternatrive you can use the Match method and then compare the input
string with the match content but that seems like a lot of trouble
 
Top stuff!!

Many thanks for your help.


Paul
The IsMatch method is defined as follows:
"Indicates whether the regular expression finds a match in the input
string."
This means that substring matches are allowed. In your expression there is a
termination character "$", but it is a part of a "|" alternative. To make
this work the way you expected modify the regex string like this:
Was:
[A-Za-z]{1,2}[1-9][0-9]?[A-Za-z]? [0-9][A-Za-z]{2}|GIR 0AA$
Should be
^[A-Za-z]{1,2}[1-9][0-9]?[A-Za-z]? [0-9][A-Za-z]{2}$|GIR 0AA$

As an alternatrive you can use the Match method and then compare the input
string with the match content but that seems like a lot of trouble

Hi all

Was wondering of anyone can help me with why the following expression
works as expected in XMLSpy, but does not in VB.net:

[A-Za-z]{1,2}[1-9][0-9]?[A-Za-z]? [0-9][A-Za-z]{2}|GIR 0AA$

IN VB.net using Regex.IsMatch, all works OK appart from the fact that it
will allow more than 2 characters in the last section. In XMLSpy the
expression is evaluated correctly and shows an error when validating an
XML element. EG.:

Vb.net: AB1 1ABB does not generate an error
XMLSpy: AB1 1ABB generates value does not match facet pattern.

Can this be confirmed as a bug in VB?


Many thanks.

Paul
 
Back
Top