How to handle infinite matches from custom regular expressions

Consider the following two lines in C # (using .NET 3.5)

Regex regex = new Regex(@"^((E|e)t )?(M|m)oi (?<NewName>[A-Za-z]\.?\w*((\-|\s)?[A-Za-z]?\w{1,})+)$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
Match m = regex.Match("moi aussi jaimerai etre un ordinateur pour pas m'énnerver ");

(sorry, this is a French program :))

When they are executed, the process gets stuck in the method Match()and never exits. I assume that there is some problem with white space in the regular expression pattern, but what I would like to do is not change the pattern (in fact, it was installed outside the program by the end users of my tool), but the ability to stop the process (for example, with timeout).

Does anyone know if this is a known issue with .NET Regular Expression, and if there is an easy way to get around it, or do I need to scroll through these lines and break them if necessary (I definitely won't like this).

+3
source share
5 answers

I think you just need to run the Regex match in a separate thread and allow it to break if a certain maximum period is reached.

+1
source

If I enter an expression in Regexbuddy, the following message will appear in it

The match attempt was interrupted earlier because the regular expression is also complex. The regular expression engine that you plan to use it may not be able to handle this at all and crash. Take a look at the "catastrophic rollback" in the help file to find out how to avoid this situation.

Search for catastrophic backtracking gives the following explanation

:
(x + x +) + y. xx + y, : , "" , , "". HTML .

, , xxxxxxxxxxy. x + 10 x. x + . + 9 , . . , +. , . Y y, . , . .

, y . y , . , . x + x, . x + x. x + xx. , , y . , x + , x. . x + , - . , x + 7 . + xxx. y, x + xx, x. , x x+. (7,1), (1,1) . (6,4), (6,2) (1,1) (6,1), (2,1), (6,1), (1,2), , , .

10x RegexBuddy 2558 , y , . 11x 5118 . 12, 10238 . , O (2 ^ n). 21x 2.8 , .

RegexBuddy , , , . (,.NET) , (, Perl, 5.10). Windows, . , - , . .

, . Regexbuddy , .

+4

, . hjusing , Regulator.

-1

, "" Regex, ( - ).

, , Regex .

-1

, . . BCL.

The best solution is to set a timeout in regular expression, not messing with threads.

See here how to disable timeout lines .

-1
source

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


All Articles