Re-matching takes a very long time to complete

I wrote a regular expression that parses the file path to another group (DRIVE, DIR, FILE, EXTENSION).

^((?<DRIVE>[a-zA-Z]):\\)*((?<DIR>[a-zA-Z0-9_]+(([a-zA-Z0-9_\s_\-\.]*[a-zA-Z0-9_]+)|([a-zA-Z0-9_]+)))\\)*(?<FILE>([a-zA-Z0-9_]+(([a-zA-Z0-9_\s_\-\.]*[a-zA-Z0-9_]+)|([a-zA-Z0-9_]+))\.(?<EXTENSION>[a-zA-Z0-9]{1,6})$))

I did a test in C #. When the path I want to check is correct. The result is very fast, and this is what I wanted to expect.

string path = @"C:\Documents and Settings\jhr\My Documents\Visual Studio 2010\Projects\FileEncryptor\Dds.FileEncryptor\Dds.FileEncryptor.csproj";

=> OK

But when I try to check a path that I know that will not match, for example:

string path = @"C:\Documents and Settings\jhr\My Documents\Visual Studio 2010\Projects\FileEncryptor\Dds.FileEncryptor\Dds.FileEncryptor?!??????";

=> ERROR

The test freezes when I call this piece of code

Match match = s_fileRegex.Match(path);

When I look at my Process Explorer, I see that the QTAgent32.exe process is hanging on 100% of my processor. What does it mean?

+3
source share
4 answers

, , , string, - backtracking .NET.

, * . * "concatenate" - "0 ". , *:

((?<DRIVE>[a-zA-Z]):\\)*

. ?, , , . , , .

+8

, , , , , .

,

(?<FILE>
  ([a-zA-Z0-9_]+
    (
      ([a-zA-Z0-9_\s_\-\.]*[a-zA-Z0-9_]+)
    |
      ([a-zA-Z0-9_]+)
    )\.
    (?<EXTENSION>[a-zA-Z0-9]{1,6})
  $)
)

,

([a-zA-Z0-9_\s_\-\.]*[a-zA-Z0-9_]+)
|
([a-zA-Z0-9_]+)

, .

, , regex , , , , , , regex engine . RegexBuddy, , 1.000.000 . # , , 100% .

, , , , , , , .

, , , .NET.

+6

FileInfo Path .

, , . .

0

Source: https://habr.com/ru/post/1757957/


All Articles