The Regex problem

A

ad

I am useing VS2005 to develop wep application.
I use a RegularExpress both in RegularExpressionValidator and Regex class to
validate a value.
The RegularExpress is 20|\-9|\-1|[1]?\d{1}

When I enter 33 and validate with RegularExpressionValidator, it fail to
pass.
But when I validate with regex class :
Regex.IsMatch(Sight0L, @"20|\-9|\-1|[1]?\d{1}");

it passed!!

Why, the results are different?
 
J

Jesse Houwing

* ad wrote, On 19-7-2006 23:52:
I am useing VS2005 to develop wep application.
I use a RegularExpress both in RegularExpressionValidator and Regex class to
validate a value.
The RegularExpress is 20|\-9|\-1|[1]?\d{1}

When I enter 33 and validate with RegularExpressionValidator, it fail to
pass.
But when I validate with regex class :
Regex.IsMatch(Sight0L, @"20|\-9|\-1|[1]?\d{1}");

it passed!!

Why, the results are different?


RegEx.IsMatch uses the .Net regular expression library while the
clientside validator will use the Javascript library. You should be able
to simulate the Javascriptlibary by setting the RegexOptions.ECMAScript

Regex.IsMatch(Sight0L, @"20|\-9|\-1|[1]?\d{1}", RegexOptions.ECMAScript);

Note that your regex contains a few errors that might be causing this:
(20|-9|-1|1?\d) is probably closer to your needs.

Note that:
'-' is no special character except in character classes (between [ and
]) so no escaping is needed here

[1] is the same as 1, so you can lose the [ and ].

\d{1} is the same as \d, so you can lose the {1}.

It is best to add ( and ) when you're using the '|'.

With these changes both libraries should give the same result.

Jesse Houwing
 
A

ad

Thanks,
My intention of RegularExpress is to test a integer value, if the value :
if ((iSight<=20 && iSight>=0) | (iSight==-1 | iSight==-9)) then pass

When I test with value 44,
My 20|\-9|\-1|[1]?\d{1} will faill when use RegularExpressionValidator but
passed with Regex class.
I have tried the regular expressin you mentioned about, but it is passed too
when when with Regex class.



Jesse Houwing said:
* ad wrote, On 19-7-2006 23:52:
I am useing VS2005 to develop wep application.
I use a RegularExpress both in RegularExpressionValidator and Regex class
to validate a value.
The RegularExpress is 20|\-9|\-1|[1]?\d{1} When I enter 33 and validate
with RegularExpressionValidator, it fail to pass.
But when I validate with regex class :
Regex.IsMatch(Sight0L, @"20|\-9|\-1|[1]?\d{1}");

it passed!!

Why, the results are different?


RegEx.IsMatch uses the .Net regular expression library while the
clientside validator will use the Javascript library. You should be able
to simulate the Javascriptlibary by setting the RegexOptions.ECMAScript

Regex.IsMatch(Sight0L, @"20|\-9|\-1|[1]?\d{1}", RegexOptions.ECMAScript);

Note that your regex contains a few errors that might be causing this:
(20|-9|-1|1?\d) is probably closer to your needs.

Note that:
'-' is no special character except in character classes (between [ and ])
so no escaping is needed here

[1] is the same as 1, so you can lose the [ and ].

\d{1} is the same as \d, so you can lose the {1}.

It is best to add ( and ) when you're using the '|'.

With these changes both libraries should give the same result.

Jesse Houwing
 
J

Jesse Houwing

* ad wrote, On 20-7-2006 1:53:
Thanks,
My intention of RegularExpress is to test a integer value, if the value :
if ((iSight<=20 && iSight>=0) | (iSight==-1 | iSight==-9)) then pass

When I test with value 44,
My 20|\-9|\-1|[1]?\d{1} will faill when use RegularExpressionValidator but
passed with Regex class.
I have tried the regular expressin you mentioned about, but it is passed too
when when with Regex class.

Ok, just add ^ and $ to contrain the regex to the whole input like this:

bool x = Regex..IsMatch("44", @"^(20|-9|-1|1?\d)$");

It should do the trick.
Jesse Houwing said:
* ad wrote, On 19-7-2006 23:52:
I am useing VS2005 to develop wep application.
I use a RegularExpress both in RegularExpressionValidator and Regex class
to validate a value.
The RegularExpress is 20|\-9|\-1|[1]?\d{1} When I enter 33 and validate
with RegularExpressionValidator, it fail to pass.
But when I validate with regex class :
Regex.IsMatch(Sight0L, @"20|\-9|\-1|[1]?\d{1}");

it passed!!

Why, the results are different?

RegEx.IsMatch uses the .Net regular expression library while the
clientside validator will use the Javascript library. You should be able
to simulate the Javascriptlibary by setting the RegexOptions.ECMAScript

Regex.IsMatch(Sight0L, @"20|\-9|\-1|[1]?\d{1}", RegexOptions.ECMAScript);

Note that your regex contains a few errors that might be causing this:
(20|-9|-1|1?\d) is probably closer to your needs.

Note that:
'-' is no special character except in character classes (between [ and ])
so no escaping is needed here

[1] is the same as 1, so you can lose the [ and ].

\d{1} is the same as \d, so you can lose the {1}.

It is best to add ( and ) when you're using the '|'.

With these changes both libraries should give the same result.

Jesse Houwing
 
H

Hans Kesting

I am useing VS2005 to develop wep application.
I use a RegularExpress both in RegularExpressionValidator and Regex class to
validate a value.
The RegularExpress is 20|\-9|\-1|[1]?\d{1}

When I enter 33 and validate with RegularExpressionValidator, it fail to
pass.
But when I validate with regex class :
Regex.IsMatch(Sight0L, @"20|\-9|\-1|[1]?\d{1}");

it passed!!

Why, the results are different?

The RegularExpressionValidator requires the entire string to be
matched. In effect it matches against "^<your RE>$".
When you use Regex.Match, it matches on a single "3", not on the entire
string. It matches the [1]?\d{1} part, because the {1}? is optional.

Hans Kesting
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top