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

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

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


当前回答

正则表达式允许您以紧凑的方式编写自定义有限状态机(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的最佳有效和正常用法是用于电子邮件地址格式验证。

这是一个很好的应用。

我曾经无数次在TextPad中一次性使用正则表达式来编辑平面文件、创建csv文件、创建SQL插入语句等等。

写得好的正则表达式不应该太慢。通常替代选项,比如大量的Replace调用,都是非常慢的选项。还不如一口气搞定。

许多情况只需要正则表达式,而不需要其他任何东西。

用无害字符替换特殊的非打印字符是另一种很好的用法。

我当然可以想象,有一些代码库过度使用正则表达式,从而损害了可维护性。我自己从来没见过。实际上,我因为没有充分使用正则表达式而被代码评审员避而远之。

我认为学习正则表达式和保持正则表达式不受欢迎, 大多数开发人员都很懒,或者他们中的大多数人都依赖于外部库来为他们做解析……他们依赖谷歌来获得答案,甚至在论坛上询问他们问题的完整代码。 但当涉及到实现或修改/维护正则表达式时,它们就会失败。

有一个流行的说法是“朋友不让朋友使用Regex来解析HTML”

但就我而言,我已经使用Regex制作了完整的HTML解析器,我发现我自己,Regex在解析HTML字符串方面更好,无论是速度方面还是内存方面(如果你有一个想法,你要实现什么:))

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

正则表达式允许您以紧凑的方式编写自定义有限状态机(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?

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

我认为这是程序员中鲜为人知的技术。因此,它并没有被广泛接受。如果你有一个非技术经理来审查你的代码或工作,那么正则表达式是非常糟糕的。你会花几个小时写一个完美的正则表达式,而你会认为他/她写了这么少的代码,你会为这个模块得到很少的分数。 此外,正如在其他地方所说,读取正则表达式是非常困难的任务。