By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,457 Members | 1,303 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,457 IT Pros & Developers. It's quick & easy.

Bug in regex called by validatingreader

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.