Regex match take a very long time to execute

前端 未结 4 1041
轻奢々
轻奢々 2020-12-18 06:08

I wrote a regular expression that parses a file path into different group (DRIVE, DIR, FILE, EXTENSION).

^((?[a-zA-Z]):\\\\)*((?[a-zA         


        
4条回答
  •  盖世英雄少女心
    2020-12-18 06:42

    The problem you are experiencing is called catastrophic backtracking and is due to the large number of ways that you regular expression can match the start of the string, which gives slow performance due to the backtracking regular expression engine in .NET.

    I think you are using * too frequently in your regular expression. * does not mean "concatenate" - it means "0 or more times". For example there should not be a * here:

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

    There should be at most one drive specification. You should use ? instead here, or else no quantifier at all if you want the drive specification to be compulsory. Similarly there appear to be other places in your regular expression where the quantifier is incorrect.

提交回复
热议问题