.Net中的XSD正则表达式模式导致应用程序挂起
发布时间:2020-12-14 05:49:33 所属栏目:百科 来源:网络整理
导读:处理时间加倍,因为“Y”向右移动. 谁能告诉我为什么?如何解决这个问题呢? 我有很多大的ID存储在数据库中,这些ID无法更改,所以我不能过多限制大小. using System;using System.IO;using System.Text;using System.Xml;using System.Xml.Schema;namespace Te
处理时间加倍,因为“Y”向右移动.
谁能告诉我为什么?如何解决这个问题呢? 我有很多大的ID存储在数据库中,这些ID无法更改,所以我不能过多限制大小. using System; using System.IO; using System.Text; using System.Xml; using System.Xml.Schema; namespace TestRegex { class Program { static void Main(string[] args) { DateTime start = DateTime.Now; /****************************************** * ID to validate ******************************************/ //string id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // Ok: Fast string id = "xxxxxxxxxxxxxxxxxxxxxYxxxxxxx"; // Invalid: Slow //string id = "xxxxxxxxxxxxxxxxxxxxxxYxxxxxx"; // Invalid: Slower //string id = "xxxxxxxxxxxxxxxxxxxxxxxYxxxxx"; // Invalid: Very slow //string id = "xxxxxxxxxxxxxxxxxxxxxxxxYxxxx"; // Invalid: Very very slow /****************************************** * XML to validate ******************************************/ XmlDocument doc = new XmlDocument(); doc.LoadXml("<root id='" + id + "'></root>"); /****************************************** * XSD validator ******************************************/ string xsl = @" <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' elementFormDefault='unqualified' attributeFormDefault='unqualified'> <xs:simpleType name='id'> <xs:restriction base='xs:string'> <xs:pattern value='^([a-z_]+[0-9]*)+' /> </xs:restriction> </xs:simpleType> <xs:element name='root'> <xs:complexType> <xs:attribute name='id' use='required' type='id' /> </xs:complexType> </xs:element> </xs:schema> "; /****************************************** * Adds XSD to XML and validates it ******************************************/ XmlTextReader reader = new XmlTextReader( new MemoryStream(ASCIIEncoding.Default.GetBytes(xsl))); XmlSchema schema = XmlSchema.Read(reader,new ValidationEventHandler(Validate)); doc.Schemas.Add(schema); doc.Validate(new ValidationEventHandler(Validate)); /****************************************** * Performance results ******************************************/ Console.WriteLine(id.Length + " = " + (DateTime.Now - start).TotalSeconds); Console.Read(); } private static void Validate(object o,ValidationEventArgs args) { if (args.Exception != null) { Console.WriteLine(args.Exception); } } } } 解决方法
这看起来像
Catastrophic Backtracking的情况.
你的正则表达式似乎过于复杂.如果我正确地读它,它接受小写和数字,当第一个字母不是数字时.您可以将其重写为: ^[a-z_]w* (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |