在探索正则表达式(也称为正则表达式)时,有许多人似乎将正则表达式视为圣杯。看起来如此复杂的东西——一定是任何问题的答案。他们倾向于认为每个问题都可以用正则表达式解决。

另一方面,也有很多人不惜一切代价试图避免正则表达式。他们试图找到一种绕过正则表达式的方法,并仅仅为了它而接受额外的编码,即使正则表达式将是一个更紧凑的解决方案。

为什么正则表达式被认为如此有争议?关于它们的工作原理是否存在广泛的误解?或者正则表达式通常比较慢是一种普遍的看法?


当前回答

正则表达式允许您以紧凑的方式编写自定义有限状态机(FSM)来处理输入字符串。为什么使用正则表达式很难,至少有两个原因:

Old-school software development involves a lot of planning, paper models, and careful thought. Regular expressions fit into this model very well, because to write an effective expression properly involves a lot of staring at it, visualizing the paths of the FSM. Modern software developers would much rather hammer out code, and use a debugger to step through execution, to see if the code is correct. Regular expressions do not support this working style very well. One "run" of a regular expression is effectively an atomic operation. It's hard to observe stepwise execution in a debugger. It's too easy to write a regular expression that accidentally accepts more input than you intend. The value of a regular expression isn't really to match valid input, it's to fail to match invalid input. Techniques to do "negative tests" for regular expressions are not very advanced, or at least not widely used. This goes to the point of regular expressions being hard to read. Just by looking at a regular expression, it takes a lot of concentration to visualize all possible inputs that should be rejected, but are mistakenly accepted. Ever try to debug someone else's regular expression code?

如果现在软件开发人员对使用正则表达式有抵触情绪,我认为主要是由于这两个因素。

其他回答

regex是一个很棒的工具,但是人们认为“嘿,多么棒的工具,我要用它来做X!”而X是另一个工具更适合做的事情(通常是解析器)。这是标准的使用锤子,你需要一个螺丝刀的问题。

问题是正则表达式潜在地非常强大,以至于可以使用它们做一些应该使用不同的东西来做的事情。

一个好的程序员应该知道在什么地方使用它们,在什么地方不使用。典型的例子是解析非常规语言(请参阅确定一种语言是否为常规语言)。

我认为如果一开始就限制自己使用真正的正则表达式(没有扩展),就不会出错。一些扩展可以使您的工作更简单一些,但是如果您发现一些很难用真正的正则表达式来表达的东西,这很可能表明正则表达式不是正确的工具。

几乎我认识的每个经常使用正则表达式的人(双关语)都有unix背景,他们使用的工具将正则视为一级编程结构,比如grep、sed、awk和Perl。由于使用正则表达式几乎没有语法开销,因此它们的效率会大大提高。

相比之下,使用正则作为外部库的语言的程序员往往不会考虑正则表达式可以带来什么。程序员的“时间成本”是如此之高,以至于a) REs从未出现在他们的培训中,b)他们不会从REs的角度“思考”,而是更喜欢使用更熟悉的模式。

我不认为人们反对正则表达式是因为它们很慢,而是因为它们很难读和写,而且很难正确。虽然在某些情况下,正则表达式为问题提供了一种有效的、紧凑的解决方案,但有时它们会被硬塞到使用易于阅读、可维护的代码部分更好的情况中。

正则表达式允许您以紧凑的方式编写自定义有限状态机(FSM)来处理输入字符串。为什么使用正则表达式很难,至少有两个原因:

Old-school software development involves a lot of planning, paper models, and careful thought. Regular expressions fit into this model very well, because to write an effective expression properly involves a lot of staring at it, visualizing the paths of the FSM. Modern software developers would much rather hammer out code, and use a debugger to step through execution, to see if the code is correct. Regular expressions do not support this working style very well. One "run" of a regular expression is effectively an atomic operation. It's hard to observe stepwise execution in a debugger. It's too easy to write a regular expression that accidentally accepts more input than you intend. The value of a regular expression isn't really to match valid input, it's to fail to match invalid input. Techniques to do "negative tests" for regular expressions are not very advanced, or at least not widely used. This goes to the point of regular expressions being hard to read. Just by looking at a regular expression, it takes a lot of concentration to visualize all possible inputs that should be rejected, but are mistakenly accepted. Ever try to debug someone else's regular expression code?

如果现在软件开发人员对使用正则表达式有抵触情绪,我认为主要是由于这两个因素。