Regex question

D

D.J Bonlay

Hi,

I am testing a regex string value and I am getting discrepancies in the resuls

If I use the following string in C# for a validation control:

RegularExpressionValidator3.ValidationExpression = "(?=.*\\d)[\\w]{8,8}";

I get different results than what I expect and from the results from regex
tester programs such as Expresso. I am checking for a string 8 characters
long that has a digit it.

here are some vectors to test the string they pass with regex testers such
as Expresso etc etc.. but it fails in my code.. I do not understand since
the Regex syntax is the same

1aaaaaaa passes fails in mycode
a1aaaaaa passes fails in mycode
aa1aaaaa -- --
aaa1aaaa -- --
aaaa1aaa -- --
aaaab1bb -- --
aaaabb1b -- --
aaaabbb1 passes fails

1111aaaa passes passes
aaaa1111 passes fails in my code

Can anyone help me - thanks
 
J

Jesse Houwing

Hello D.J Bonlay D.J,
Hi,

I am testing a regex string value and I am getting discrepancies in
the resuls

If I use the following string in C# for a validation control:

RegularExpressionValidator3.ValidationExpression =
"(?=.*\\d)[\\w]{8,8}";

I get different results than what I expect and from the results from
regex tester programs such as Expresso. I am checking for a string 8
characters long that has a digit it.

here are some vectors to test the string they pass with regex testers
such as Expresso etc etc.. but it fails in my code.. I do not
understand since the Regex syntax is the same

This could be because in code it actually uses ECMAScript mode and Javascript
on the clientside. If you hadn't tested that setting and that engine the
results will always be unpredictable. That is the problem with the fact that
every programming environment has their own flavour of regex. They're all
almost equal, but all quite different.
1aaaaaaa passes fails in mycode
a1aaaaaa passes fails in mycode
aa1aaaaa -- --
aaa1aaaa -- --
aaaa1aaa -- --
aaaab1bb -- --
aaaabb1b -- --
aaaabbb1 passes fails
1111aaaa passes passes
aaaa1111 passes fails in my code
Can anyone help me - thanks

instead of [\\w] try \w, I think that Javascript interprets that differently
than .NET.

And because you know that the first part will never be longer than 7+one
digit, you should rewrite that to:
^(?=.{0,7}\d)\w{8,8}$
For performance reasons.

This might also work:
^(?=.{8}$)\w{0,7}\d{1,8}\w{0,7}$

And finally you could also use 2 validators, one to validate the length and
a second to validate the pattern. That way you can just use these two patterns:
^.{8}$
^\w{0,7}\d{1,8}\w{0,7}$
 
Top