正则表达式匹配单点而不匹配两个点? [英] Regular expression to match single dot but not two dots?

查看:690
本文介绍了正则表达式匹配单点而不匹配两个点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试创建用于电子邮件地址检查的正则表达式模式.这将允许一个点(.),但如果相邻的点不止一个.

Trying to create a regex pattern for email address check. That will allow a dot (.) but not if there are more than one next to each other.

应匹配: test.test@test.com

Should match: test.test@test.com

不应匹配: test..test @ test.com

Should not match: test..test@test.com

现在,我知道互联网上有成千上万个用于电子邮件匹配的示例,所以请不要向我发布包含完整解决方案的链接,我正在尝试在这里学习.

Now I know there are thousands of examples on internet for e-mail matching, so please don't post me links with complete solutions, I'm trying to learn here.

实际上,最让我感兴趣的部分只是本地部分: 应该匹配的test.test和应该不匹配的test..test. 感谢您的帮助.

Actually the part that interests me the most is just the local part: test.test that should match and test..test that should not match. Thanks for helping out.

推荐答案

您可以使用在它们之间进行分离(管道符号|),并将带有*(其中任意数量)的整个内容放在^$之间,以便整个字符串由这些组成.这是代码:

You may allow any number of [^\.] (any character except a dot) and [^\.])\.[^\.] (a dot enclosed by two non-dots) by using a disjunction (the pipe symbol |) between them and putting the whole thing with * (any number of those) between ^ and $ so that the entire string consists of those. Here's the code:

$s1 = "test.test@test.com";
$s2 = "test..test@test.com";
$pattern = '/^([^\.]|([^\.])\.[^\.])*$/';
echo "$s1: ", preg_match($pattern, $s1),"<p>","$s2: ", preg_match($pattern, $s2);

收益:

test.test@test.com: 1
test..test@test.com: 0

这篇关于正则表达式匹配单点而不匹配两个点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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