C# 正则表达式中环视的效率。如果可以的话我应该避开它们吗?
每个人!我对正则表达式还很陌生,但我非常喜欢它们!
如果你愿意的话,可以叫我吹毛求疵,但我真的很想知道如果我有选择的话,我是否应该避免使用前瞻和后视。
例如,下面的两个命令执行相同的操作,一个使用lookbehind,另一个则不使用。
the_str = Regex.Replace(the_str, @"(;|!|\?) \.{3}", "$1...");
the_str = Regex.Replace(the_str, @"(?<=(;|!|\?)) \.{3}", "...");
您会使用哪一个?哪个更有效率?
感谢您的回答!
everyone! I'm quite new to regular expressions, but I like them, A LOT!
Call me nitpicky if you will, but I'd really like to know if I should avoid using lookaheads and lookbehinds if I have an option.
For example, the two commands below do the same thing, one uses lookbehind and the other doesn't.
the_str = Regex.Replace(the_str, @"(;|!|\?) \.{3}", "$1...");
the_str = Regex.Replace(the_str, @"(?<=(;|!|\?)) \.{3}", "...");
Which one would you use? Which is more efficient?
Thanks for your answers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在本地进行了测试,使用lookbehind 的方法大约慢了 25%。
我使用前向而不是后向测试的另一种变体仅慢了 10%:
我认为没有足够的性能差异来建议始终避免环视。如果您认为它使代码更具可读性,那么请使用它们。仅当分析显示您存在性能问题并且正则表达式是瓶颈时才优化性能。
作为信息,我测试的字符串是
"blah; ... foo ...; bar bar ? ..."
重复了 1000 次,并且我将每个测试重复了 100 次。I tested both locally and the method using a lookbehind was about 25% slower.
Another variation I tested using a lookahead instead of a lookbehind was only 10% slower:
I don't think there's enough of a performance difference to advise always avoiding lookarounds. If you think it makes the code more readable then do use them. Only optimize for performance if profiling shows that you have performance problem and the regular expression is the bottleneck.
For information, the string I tested on was
"blah; ... foo ...; bar bar ? ..."
repeated 1000 times and I repeated each test 100 times.