C# Regex Performance very slow

前端 未结 3 518
醉梦人生
醉梦人生 2020-12-29 08:53

I am very new in regex topic. I want to parse log files with following regex:

(?
3条回答
  •  抹茶落季
    2020-12-29 09:30

    Let me "convert" my comment into an answer since now I see what you can do about the regex performance.

    As I have mentioned above, replace all .*? with [^|]*, and also all repeating [|][|][|] with [|]{3} (or similar, depending on the number of [|]. Also, do not use nested capturing groups, that also influences performance!

    var logFileFormat = @"(?

    Only the last .* can remain "wildcardish" since it will grab the rest of the line.

    Here is a comparison of your and my regex patterns at RegexHero.

    Then, use RegexOptions.Compiled:

    Regex pattern = new Regex(LogFormat.GetLineRegex(logFileFormat), RegexOptions.Compiled);
    

提交回复
热议问题