regex - Efficiency of lookarounds in C# regular expressions. Should I avoid them if I can? -
everyone! i'm quite new regular expressions, them, lot!
call me nitpicky if will, i'd know if should avoid using lookaheads , lookbehinds if have option.
for example, 2 commands below same thing, 1 uses lookbehind , other doesn't.
the_str = regex.replace(the_str, @"(;|!|\?) \.{3}", "$1..."); the_str = regex.replace(the_str, @"(?<=(;|!|\?)) \.{3}", "...");
which 1 use? more efficient?
thanks answers!
i tested both locally , method using lookbehind 25% slower.
another variation tested using lookahead instead of lookbehind 10% slower:
s = regex.replace(s, @"(;|!|\?) (?=\.{3})", "$1");
i don't think there's enough of performance difference advise avoiding lookarounds. if think makes code more readable use them. optimize performance if profiling shows have performance problem , regular expression bottleneck.
for information, string tested on "blah; ... foo ...; bar bar ? ..."
repeated 1000 times , repeated each test 100 times.
0.944s no lookarounds regex.replace(s, @"(;|!|\?) \.{3}", "$1...") 1.027s ahead regex.replace(s, @"(;|!|\?) (?=\.{3})", "$1") 1.210s behind regex.replace(s, @"(?<=(;|!|\?)) \.{3}", "...") 1.124s both regex.replace(s, @"(?<=(;|!|\?)) (?=\.{3})", "")
Comments
Post a Comment