नीचे दिए गए कोड में सी # स्ट्रिंग अक्षर निकालने के लिए डिज़ाइन की गई एक नियमित अभिव्यक्ति होती है लेकिन कुछ वर्णों से अधिक इनपुट स्ट्रिंग के लिए रेगेक्स मिलान का प्रदर्शन दुखी है।धीमी रेगेक्स प्रदर्शन
class Program
{
private static void StringMatch(string s)
{
// regex: quote, zero-or-more-(zero-or-more-non-backslash-quote, optional-backslash-anychar), quote
Match m = Regex.Match(s, "\"(([^\\\\\"]*)(\\\\.)?)*\"");
if (m.Success)
Trace.WriteLine(m.Value);
else
Trace.WriteLine("no match");
}
public static void Main()
{
// this first string is unterminated (so the match fails), but it returns instantly
StringMatch("\"OK");
// this string is terminated (the match succeeds)
StringMatch("\"This is a longer terminated string - it matches and returns instantly\"");
// this string is unterminated (so the match will fail), but it never returns
StringMatch("\"This is another unterminated string and takes FOREVER to match");
}
}
मैं एक अलग रूप में regex refactor कर सकते हैं, लेकिन किसी को एक स्पष्टीकरण क्यों प्रदर्शन इतना बुरा है की पेशकश कर सकते हैं?
http://msdn.microsoft.com/en-us/magazine/ff646973.aspx – SLaks
मुझे लगता है कि यह गलत है। '[^ \"] '' \ "' पर नहीं रुक जाएगा। यह '\' या '' 'पर बंद हो जाएगा। तो यह' \ n' '\ n' पर बंद हो जाएगा। क्या यह सही है? – xanatos
यदि आप बैक्रेरेंस का उपयोग नहीं कर रहे हैं तो शायद आप अपने रेगेक्स को संशोधित कर सकते हैं।" \ "(? (? [^ \\\"] *) (:।? \\)) * \ "" '। बेशक यदि आप बैक्रेरेंस का उपयोग कर रहे हैं, तो इसे अनदेखा करें। – Matthew