Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

Regex question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
D.J Bonlay - 06 Feb 2008 20:16 GMT
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
Jesse Houwing - 06 Feb 2008 23:01 GMT
Hello D.J Bonlay D.J,

> Hi,
>
[quoted text clipped - 13 lines]
> 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
[quoted text clipped - 7 lines]
> 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}$

--
Jesse Houwing
jesse.houwing at sogeti.nl

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.