为什么人们使用regexp进行电子邮件和其他复杂的验证? [英] Why are people using regexp for email and other complex validation?

查看:151
本文介绍了为什么人们使用regexp进行电子邮件和其他复杂的验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有许多电子邮件正则表达式问题弹出 up 在这里,我老实说,为什么人们使用这些疯狂地钝点匹配表达式,而不是一个非常简单的解析器,分割邮件然后验证这些名称和域名所允许的有效字符(这个部分没有进一步的检查)和域的有效字符(我想你可以添加所有的世界顶级域名(TLD),然后是另一级二级域名有这样的国家(即com.uk))。

There are a number of email regexp questions popping up here, and I'm honestly baffled why people are using these insanely obtuse matching expressions rather than a very simple parser that splits the email up into the name and domain tokens, and then validates those against the valid characters allowed for name (there's no further check that can be done on this portion) and the valid characters for the domain (and I suppose you could add checking for all the world's TLDs, and then another level of second level domains for countries with such (ie, com.uk)).

真正的问题是,tlds和slds不断变化(与普遍的信念相反),所以你必须保持如果您计划在根名称服务器发送更改时计划执行所有这些高级检查,请更新正则表达式。

The real problem is that the tlds and slds keep changing (contrary to popular belief), so you have to keep updating the regexp if you plan on doing all this high level checking whenever the root name servers send down a change.

为什么没有一个模块只是验证域,从数据库或平面文件,并可选地检查DNS匹配记录?

Why not have a module that simply validates domains, which pulls from a database, or flat file, and optionally checks DNS for matching records?

我在这里很认真,为什么每个人都热衷于为这个发明完美的正则表达式?这似乎不是解决这个问题的一个合适的解决方案。

I'm being serious here, why is everyone so keen on inventing the perfect regexp for this? It doesn't seem to be a suitable solution to the problem...

告诉我,不仅可以在regexp(并满足每个人),而且它是比自定义解析器/验证器更好的解决方案。

Convince me that it's not only possible to do in regexp (and satisfy everyone) but that it's a better solution than a custom parser/validator.

-Adam

推荐答案

p>他们这样做是因为他们看到我想测试这个文本是否符合规范,并立即认为我知道,我会使用正则表达式!没有充分了解规范的复杂性或正则表达式的限制。正则表达式是处理各种文本匹配任务的精美功能强大的工具,但它们不是每个这样的任务的完美工具,似乎许多使用它们的人忽视了这一点。

They do it because they see "I want to test whether this text matches the spec" and immediately think "I know, I'll use a regex!" without fully understanding the complexity of the spec or the limitations of regexes. Regexes are a wonderful, powerful tool for handling a wide variety of text-matching tasks, but they are not the perfect tool for every such task and it seems that many people who use them lose sight of that fact.

这篇关于为什么人们使用regexp进行电子邮件和其他复杂的验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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