473,548 Members | 2,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Bug in regex called by validatingreade r

Hey MS, here's an apparent problem in the base class library. I pulled
the email validation pattern
"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@(([0-9a-zA-Z])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,9})"
from http://regexlib.com.

If I validate the email address "te**@someveryl ongemailaddress .com"
against it by just creating a RegEx and calling IsMatch it works fine,
but if I create a schema defining a simple type restricting an
xs:string by the regex pattern, it takes over full minute at 100% cpu
to match. This is true either when validating from the vs.net ide or
from an executable instantiating a validatingreade r. The time to match
seems to be exponential against the length of the text.

I'm no regex expert, so please let me know if this is just a problem
with the expression, and if so what a good email address validating
expression would be. We need full unicode support in the email
addresses.

Here's a doc and some schema to reproduce. Just open both in the ide
and then use the vs.net menu to validate the xml.

<?xml version="1.0" encoding="utf-8" ?>
<email xmlns="http://tempuri.org/XMLSchema1.xsd"
value="te**@som everylongemaila ddress.com" />
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="XMLSchema1 "
targetNamespace ="http://tempuri.org/XMLSchema1.xsd"
elementFormDefa ult="qualified"
xmlns="http://tempuri.org/XMLSchema1.xsd"
xmlns:mstns="ht tp://tempuri.org/XMLSchema1.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="email">
<xs:complexType >
<xs:attribute name="value">
<xs:simpleTyp e>
<xs:restricti on base="xs:string ">
<xs:pattern value="^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@(([0-9a-zA-Z])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,9})"
/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:schema>
Nov 12 '05 #1
1 1709
"Colin Reid" <cr***@projux.c om> wrote in message news:10******** *************** **@posting.goog le.com...
If I validate the email address "te**@someveryl ongemailaddress .com"
against it by just creating a RegEx and calling IsMatch it works fine,
but if I create a schema defining a simple type restricting an
xs:string by the regex pattern, it takes over full minute at 100% cpu
to match. : : The time to match seems to be exponential against the length
of the text.


I confirm seeing the same behavior.

I'd expect the validating reader would just use the same Regex,
since as you've stated, Regex::IsMatch( ) handles this expression
just fine. Perhaps something else involved in the context of an
attribute value is responsible (space-separated list, the quote
delimiter character, checking for entity references).

What can be done as a workaround in an application is to just
plant a simple, valid e-mail address expression in the schema
document you're using, and handle the match against Rob
Eberhardt's e-mail address regular expression in the Validation-
EventHandler.

Change the xsd from this,

<xs:pattern value="^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@(([0-9a-zA-Z])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,9})$" />

to this,

<xs:pattern value="^X@X.COM $" />

and initialize the XmlValidatingRe ader like so,

// . . .
ValidationEvent Handler veh = new ValidationEvent Handler(
this.Validation CallBack );

this.mValidatin gReader = new XmlValidatingRe ader(
new XmlTextReader( "XMLSchema1.xsd ") );
this.mValidatin gReader.Validat ionEventHandler += veh;

this.mEmailExpr = new Regex( "^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@(([0-9a-zA-Z])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,9})$");

this.mDocIsVali d = true;
// . . .

then in the validation callback message that XmlValidatingRe ader will
invoke when it reads an invalid attribute value,

private void ValidationCallB ack(object sender, ValidationEvent Args e)
{
if ( this.mDocIsVali d && e.Message.Start sWith( "The 'value' attribute") )
{
string attr = this.mValidatin gReader.GetAttr ibute( "value");
this.mDocIsVali d = this.mEmailExpr .IsMatch( attr);
}
else
{
this.mDocIsVali d = false;
}
}

Here when the value attribute fails to match X@X.COM it goes on
to use a pre-compiled Regex, mEmailExpr, to validate the e-mail
address. Should this pass validation, the flag mDocIsValid continues
to indicate the document as being valid.

If you're concerned about changing the XML schema document (i.e.,
you need it to validate in other XML environments), you can apply an
XslTransform to it prior to loading it into the XmlSchema you attach
to the XmlValidatingRe ader. This XslTransform would replace Rob
Eberhardt's email address regular expression with the simplified
e-mail address pattern, and the change would only affect the schema
as its seen within the application.
Derek Harmon
Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
9714
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a numeric value according to an arbitrary regular expression.
9
4565
by: Tim Conner | last post by:
Is there a way to write a faster function ? public static bool IsNumber( char Value ) { if (Regex.IsMatch( Value.ToString(), @"^+$" )) { return true; } else return false; }
3
2427
by: DDK | last post by:
I am trying to figure out how to Replace tags such as ... with the correct HTML <b>...</b> tags in C#. The code below works however only if one set of tags are found, if you have more than two sets of the tags within the text it renders from the first . Here is the code that doesn't quite work:...
20
8045
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex matches are called within a loop (like if or for). E.g. for(int i = 0; i < 10; i++) { Regex r = new Regex();
2
1286
by: Art | last post by:
Hi & help, I'm trying to parse arithmetic expressions such as 4*(7-2.1). The first thing I'm trying is to add spaces to get: 4 * ( 7 - 2.1 ). I thought that Regular Expressions would be the way to go. I tried: regex.replace("\D", " \D ") which did not work. I don't know how to refer to the string that was matched in forming the...
3
1392
by: spamsickle | last post by:
I have a Perl background, so some of what I know in other contexts is probably getting in the way of what I need to learn now. With that said, I'm having a problem getting my regex to work as I expect. I have a string value like "John Q Public" in a textbox called "Name", and I want to use the regex to split out first name and last name....
7
2047
by: Nightcrawler | last post by:
Hi all, I am trying to use regular expressions to parse out mp3 titles into three different groups (artist, title and remix). I currently have three ways to name a mp3 file: Artist - Title Artist - Title (Remix) Artist - Title
3
1643
by: Ethan Strauss | last post by:
Hi, I have written a regular expression which is supposed to pull a direction (forward or reverse) designation from a file name. Unfortunately, the direction designation can either be the whole word ("Forward" or "Reverse") or just a single letter ("F" or "R") and the rest of the name is not as consistent as I would like.. For example...
6
2057
by: | last post by:
Hi all, Sorry for the lengthy post but as I learned I should post concise-and-complete code. So the code belows shows that the execution of ValidateAddress consumes a lot of time. In the test it is called a 100 times but in my real app it could be called 50000 or more times. So my question is if it is somehow possible to speed this up...
0
7512
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7438
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7707
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7951
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5362
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5082
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3475
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.