C# 正则表达式中环视的效率。如果可以的话我应该避开它们吗?

发布于 2024-09-26 02:45:17 字数 337 浏览 1 评论 0原文

每个人!我对正则表达式还很陌生,但我非常喜欢它们!

如果你愿意的话,可以叫我吹毛求疵,但我真的很想知道如果我有选择的话,我是否应该避免使用前瞻和后视。

例如,下面的两个命令执行相同的操作,一个使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮云落日 2024-10-03 02:45:17

我在本地进行了测试,使用lookbehind 的方法大约慢了 25%。

我使用前向而不是后向测试的另一种变体仅慢了 10%:

s = Regex.Replace(s, @"(;|!|\?) (?=\.{3})", "$1");

我认为没有足够的性能差异来建议始终避免环视。如果您认为它使代码更具可读性,那么请使用它们。仅当分析显示您存在性能问题并且正则表达式是瓶颈时才优化性能。

作为信息,我测试的字符串是 "blah; ... foo ...; bar bar ? ..." 重复了 1000 次,并且我将每个测试重复了 100 次。

0.944s   No lookarounds    Regex.Replace(s, @"(;|!|\?) \.{3}", "$1...") 
1.027s   Look ahead        Regex.Replace(s, @"(;|!|\?) (?=\.{3})", "$1")
1.210s   Look behind       Regex.Replace(s, @"(?<=(;|!|\?)) \.{3}", "...")
1.124s   Both              Regex.Replace(s, @"(?<=(;|!|\?)) (?=\.{3})", "")

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:

s = Regex.Replace(s, @"(;|!|\?) (?=\.{3})", "$1");

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.

0.944s   No lookarounds    Regex.Replace(s, @"(;|!|\?) \.{3}", "$1...") 
1.027s   Look ahead        Regex.Replace(s, @"(;|!|\?) (?=\.{3})", "$1")
1.210s   Look behind       Regex.Replace(s, @"(?<=(;|!|\?)) \.{3}", "...")
1.124s   Both              Regex.Replace(s, @"(?<=(;|!|\?)) (?=\.{3})", "")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文