c# - Regex match take a very long time to execute -


I have written a regular expression that parses the file path in a different group (drive, dir, file, extension).

^ ((? <>> Drive> [A-ZA-ZED]): \\) * ((? & Lt; DIR & gt; [A-GA-Z0- 9 _] + (([A-zA-Z0-9_s_r_\\] * [one-zA-Z0-9_]) | ([one-zA-Z0-9 _]))) \\) * (. & Lt; file & gt; ([one-zA-Z0-9 _] (([a-zA-Z0-9_s _ \ - \.] * [One-zA-Z0-9 _] ) | ([A-zA-Z0-9_] +)). (? & Lt; EXTENSION & gt; [a-zA-Z0- 9] {1,6}) $))

I did an exam in C #. The path I want to test is correct. The result is very fast and this is what I wanted.

  string path = @ "C: \ documents and settings \ jr \ my document \ visual studio 2010 \ project \ file encryption \ dds .FileEncryptor \ Dds.FileEncryptor.csproj";  

=> OK

But when I try to test with the path that I know does not match, then this way:

path = @ "C: \ Documents and Settings \ jr \ My Documents \ Visual Studio 2010 \ Project \ File Encryption \ Dd .file Encryption \ Dd .file Encryption ?!";;

=> bug

When I call this part of the code, the test freezes

  matching match = s_fileRegex .Match (path);  

When I look at my process explorer, I see QTAgent32.exe hanging process at 100% of my processor. what is the meaning of this?

The problem you are experiencing is called and a large number of ways that you can get regular expressions You can match the beginning of the string, which The back tracking in the net gives slow functionality due to the regular expression engine.

I think that you are using * in a very long time. Your regular expression * does not mean "connection" - this means For example, "0 or more times" should not be * here:

  ((? & Lt; Drive & gt; [a-zA-Z]) : \\) *  

Most should be a drive specification. Are you here instead of ? , or if you want the drive specification to be compulsory, of course not at all. Similarly, there is a possibility of another place in your regular expression where the quantifier is wrong.


Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -