使用 VB.NET Like Operator 与 RegEx 相比有什么优势吗?

问题描述:

除了可能增强非常简单模式的可读性之外,为什么有人会选择在 VB.NET 中使用 Like 运算符而不是正则表达式来进行字符串模式匹配?有什么优势吗?

Other than perhaps enhanced readability for very simple patterns, why would someone choose to use the Like operator in VB.NET over regular expressions for string pattern matching? Are there any advantages?

可能.如果你想看看 Like 是如何实现的,大部分(全部?)都在 Microsoft.VisualBasic.CompilerServices.LikeOperator 中,基本可以在 #LikeObject 和 #LikeString.查看文档喜欢 显然使用了完整的正则表达式引擎的一个非常严格的子集,并且就像几乎所有与 Perl 兼容的正则表达式引擎一样,对于简单的表达式来说,有些繁重的工作可能会显得过分.

Probably. If you want to take a look at how Like is implemented, much (all?) of it is in Microsoft.VisualBasic.CompilerServices.LikeOperator, and the rudiments can be seen in #LikeObject and #LikeString. Looking at the documentation, Like obviously uses a pretty strict subset of the full-on regular expression engine, and like just about any Perl-Compatible Regular Expression engine, there's some heavy lifting that may be overkill for simple expressions.

也就是说,在我看来,这归结为风格.如果您觉得 If (myString Like "a?bb") 更具可读性、更地道,并且与您的其他代码保持一致,那就去做吧.在我看来,除了上述原因之外,无论哪种方式都是微优化剧院,特别是因为您可以 编译正则表达式(如果需要).

That said, in my opinion, it comes down to style. If you feel that If (myString Like "a?bb") is more readable, idiomatic, and consistent with the rest of your code, go for it. It would appear to me that going either way for anything but the aforementioned reasons is microoptimization theatre, especially since you can compile regexes if you need to.