Connecting Tech Pros Worldwide Help | Site Map

The Regex problem

ad
Guest
 
Posts: n/a
#1: Jul 19 '06
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?







Jesse Houwing
Guest
 
Posts: n/a
#2: Jul 19 '06

re: The Regex problem


* ad wrote, On 19-7-2006 23:52:
Quote:
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
ad
Guest
 
Posts: n/a
#3: Jul 20 '06

re: The Regex problem


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" <jesse.houwing@nospam-sogeti.nl>
???????:e4oEkA4qGHA.4632@TK2MSFTNGP05.phx.gbl...
Quote:
>* ad wrote, On 19-7-2006 23:52:
Quote:
>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

Jesse Houwing
Guest
 
Posts: n/a
#4: Jul 20 '06

re: The Regex problem


* ad wrote, On 20-7-2006 1:53:
Quote:
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.
Quote:
>
>
>
"Jesse Houwing" <jesse.houwing@nospam-sogeti.nl>
???????:e4oEkA4qGHA.4632@TK2MSFTNGP05.phx.gbl...
Quote:
>* ad wrote, On 19-7-2006 23:52:
Quote:
>>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
>
>
Hans Kesting
Guest
 
Posts: n/a
#5: Jul 20 '06

re: The Regex problem


I am useing VS2005 to develop wep application.
Quote:
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


Closed Thread