【发布时间】:2010-11-01 10:04:55
【问题描述】:
很明显,有很多问题看起来像一个简单的正则表达式就能解决,但事实证明用正则表达式很难解决。
那么,不是正则表达式专家的人如何知道他/她是否应该学习正则表达式来解决给定的问题?
(请参阅"Regex to parse C# source code to find all strings" 了解我提出这个问题的方式。)
这似乎总结得很好:
Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems...
(我刚刚更改了问题的标题以使其更具体,因为 C# 中 Regex 的一些问题在 Perl 和 JScript 中已解决,例如,两个级别的引用使 Regex 如此难以阅读.)
【问题讨论】:
-
也许另请参阅Why it's not possible to use regex to parse HTML/XML: a formal explanation in layman's terms,它专门讨论了为什么要避免使用 XML 和 HTML 等结构化格式(以及扩展 JSON、YAML、大多数语言的源代码等)的正则表达式。