473,472 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Bug in regex called by validatingreader

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**@someverylongemailaddress.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 validatingreader. 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**@someverylongemailaddress.com" />
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="XMLSchema1"
targetNamespace="http://tempuri.org/XMLSchema1.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/XMLSchema1.xsd"
xmlns:mstns="http://tempuri.org/XMLSchema1.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="email">
<xs:complexType>
<xs:attribute name="value">
<xs:simpleType>
<xs:restriction 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 1686
"Colin Reid" <cr***@projux.com> wrote in message news:10*************************@posting.google.co m...
If I validate the email address "te**@someverylongemailaddress.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 XmlValidatingReader like so,

// . . .
ValidationEventHandler veh = new ValidationEventHandler(
this.ValidationCallBack );

this.mValidatingReader = new XmlValidatingReader(
new XmlTextReader( "XMLSchema1.xsd") );
this.mValidatingReader.ValidationEventHandler += 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.mDocIsValid = true;
// . . .

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

private void ValidationCallBack(object sender, ValidationEventArgs e)
{
if ( this.mDocIsValid && e.Message.StartsWith( "The 'value' attribute") )
{
string attr = this.mValidatingReader.GetAttribute( "value");
this.mDocIsValid = this.mEmailExpr.IsMatch( attr);
}
else
{
this.mDocIsValid = 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 XmlValidatingReader. 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
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...
9
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
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...
20
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...
2
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...
3
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...
7
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 ...
3
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...
6
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...
0
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,...
0
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...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
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...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.