为什么人们捍卫正则表达式语法? [英] Why do people defend the regex syntax?

查看:56
本文介绍了为什么人们捍卫正则表达式语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个类似的问题,但它只是得到了人们总是给出的关于正则表达式语法的相同旧答案,但这不是这里的重点,所以请尽量不要下意识地使用关于正则表达式语法的相同旧答案.这次尝试更加原创和个性化.

Regex 语法非常非常紧凑,几乎太紧凑了.就像打码打高尔夫球一样,并且每个人都同意代码打高尔夫球在生产代码中不是一件好事.然而,大多数人接受正则表达式语法,这似乎……至少可以说是矛盾的.

所以现在人们可能听到的一些常见的防御包括:

  • 答案:很紧凑

  • :在这个时代,我们不是都同意代码应该是有文化的,像client"这样的变量比c"更好吗?

  • 答案:这是一种领域特定语言"

  • :那些非常容易理解、不紧凑、不神秘并且我敢说像 SQL 或 LINQ 这样漂亮的领域语言怎么样?

  • 答案:一旦你知道它就很容易理解.

  • Counter:即使您以前从未使用过,大多数优秀的语言也很容易理解.例如,任何人都可以很容易地跳入 Python,即使他们以前从未见过它.为什么当 Regex 是一种很难看的语言时,人们会为它辩护,然后继续抱怨 Lisps 括号?

好的,现在每个人都尽量保持原创和诚实,不要只是拿出 20 年前程序员在设计正则表达式时使用的那些老生常谈的死记硬背答案.除非你真的相信它们在当今时代是有效的命题.

<小时>

作为记录,我多年前就知道 Regex,即使在今天也经常使用它们,甚至可能会理解它们.然而,我突然有一种感觉,也许是时候重新考虑我认为关于正则表达式的真相",并从现代的角度来看待它们.主要是因为质疑原则是进一步发展所必需的,而且因为很多新人对他们的抱怨很激烈,他们不能一概而论,所以我决定尝试站在新人的角度考虑一下有什么好处反对正则表达式.

至于主观,我不认为这比当时的程序员笑话更主观或更少与程序员相关.相反,它与程序员非常相关.

至于争论性,这就是问题的重点.为了获得赞成和反对正则表达式过时语法的良好论据,这可以让新手真正了解为什么正则表达式是它们的原因,甚至更好的是希望让一些新人提出更好的解决方案,美国老者看不到,因为我们被正则表达式的酷"蒙蔽了双眼.

<小时>

引用:

<块引用>

Perl 5.10 文档正则表达式已经融化成一堆不可读的胡言乱语,因为太多滑稽功能已经渗透到语法中没有人可以写出明智的不再需要它的文档.

你是想说正则表达式变得不可维护?那么作为优秀的程序员,我们应该考虑重构它们吗?也许像我们对许多其他技术所做的那样进行清理和尝试?

解决方案

我要说的大部分内容都由 Adam 和 DGM 解决,但我认为他们没有很好地涵盖您的第二点.

那些非常容易理解、不紧凑、不神秘并且我敢说像 SQL 或 LINQ 这样漂亮的领域语言怎么样?"

我认为表达这个问题的一个好方法是问,你会如何用英语来解释正则表达式?

]*>(.*?)

寻找<TAG"一个词边界零个或多个不是'>'的东西,后面跟着一个'>'记住零个或多个东西,停在第一个</TAG>"

这是一个相当简单的正则表达式.英文形式真的更容易理解吗?你能做得更好吗?

正则表达式很难阅读,但你想从它们中得到什么却同样难以解释.

There is a similar question going around, but it just got the same old answers that people always give about Regex syntax, but that's not the point here, so please try to not knee jerk the same old answers about regex syntax. Try to be a little more original and personal about it this time.

Regex syntax is very VERY compact, almost too compact to be good. Its like code-golfing, and everyone agrees code-golfing isn't a good thing in production code. However most people accept regex syntax, which seems... contradictory to say the least.

So now some common defenses one is likely to hear include :

  • Answer: It's compact

  • Counter: Haven't we all agreed in this day and age that code should be literate and a variable like "client" is better than "c"?

  • Answer: It's a "domain specific language"

  • Counter: how about all the very easy to understand, non compact, non cryptic and dare I say pretty domain languages out there like SQL or LINQ?

  • Answer: Its easy to understand once you know it.

  • Counter: Most great languages are easy to understand even if you've never used them before. For example anyone could jump into Python very easily even if they had never seen it before. And why do people defend Regex when its such a hard language to look at, but then go on and complain about Lisps parenthesis?

Ok now everyone try to be original and honest here, don't just pull out the same old rote answers programmers used 20 years ago to design regex. Unless you really believe they are valid propositions in this day and age.


Edit: For the record, I know Regex from years ago, use them frequently even today and might even grok them. However I suddenly had a feeling it was perhaps time to reconsider things I had taken as "truths" about regex, and look at them from a modern standpoint. Mostly because questioning principles is necessary for further development, and because so many newcomers complain vehemently about them, they can't just be flat out right, so I decided to try to step into the shoes of a newcomer and consider what are some good points against regex.

As for being subjective, I don't think this is less subjective OR less programmer related than Programmer Jokes of the days stuff. On the contrary it is very programmer related.

As for argumentative, thats the point of the question. To get good arguments pro and con regex outdated syntax, that can serve newcomers to actually understand more about why regex are what they are, and even better hopefully get some newcomer to come up with a better solution US old minded can't see because we are blinded by the "coolness" of regex.


Quote:

The Perl 5.10 documentation for regexes has melted down into a heap of unreadable drivel because so many zany features have creeped into the syntax that no-one can write sensible documentation for it any more.

You're trying to say regex have become unmaintainable? Well then as good programmers should we consider refactoring them? Maybe cleaning up and trying over as we've done with some many other technologies?

解决方案

Most of what I have to say were addressed by Adam and DGM, but I don't think they cover your second point very well.

"how about all the very easy to understand, non compact, non cryptic and dare I say pretty domain languages out there like SQL or LINQ?"

I think a good way to express an answer to this is to ask, how would you use English to explain a regular expression?

<TAG\b[^>]*>(.*?)</TAG>

Look for "<TAG" a word boundary zero or more of something that is not '>' followed by a '>' remember zero or more of something, stopping at the first "</TAG>"

This is a fairly simple regex. Is the English form really easier to understand? Could you do better?

Regular expressions are hard to read, but what you want from them can be just as hard to explain.

这篇关于为什么人们捍卫正则表达式语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆