php 正则表达式匹配单个点而不是两个点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10673892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regular expression to match single dot but not two dots?
提问by Carbon6
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.
尝试为电子邮件地址检查创建正则表达式模式。这将允许一个点 (.) 但如果有多个彼此相邻则不允许。
Should match: [email protected]
应该匹配:[email protected]
Should not match: [email protected]
不应该匹配:[email protected]
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.
现在我知道互联网上有数以千计的电子邮件匹配示例,所以请不要将包含完整解决方案的链接张贴给我,我正在尝试在这里学习。
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.
实际上,我最感兴趣的部分只是本地部分:应该匹配的 test.test 和不应该匹配的 test..test。感谢您的帮助。
采纳答案by Junuxx
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 = "[email protected]";
$s2 = "[email protected]";
$pattern = '/^([^\.]|([^\.])\.[^\.])*$/';
echo "$s1: ", preg_match($pattern, $s1),"<p>","$s2: ", preg_match($pattern, $s2);
Yields:
产量:
[email protected]: 1
[email protected]: 0
回答by Mihai Stancu
This seams more logical to me:
这对我来说更合乎逻辑:
/[^.]([\.])[^.]/
And it's simple. The look-ahead & look-behinds are indeed useful because they don't capture values. But in this case the capture group is only around the middle dot.
这很简单。前瞻和后视确实很有用,因为它们不捕获值。但在这种情况下,捕获组仅在中间点附近。
回答by MajidTaheri
strpos($input,'..') === false
strposfunction is more simple, if `$input' has not '..' your test is success.
strpos函数更简单,如果 `$input' 没有 '..' 你的测试是成功的。
回答by Bilal Akil
^([^.]+\.?)+@$
That should do for the what comes before the @, I'll leave the rest for you.
Note that you should optimise it more to avoid other strange character setups, but this seems sufficient in answering what interests you
这应该解决 之前的事情@,剩下的交给你。请注意,您应该对其进行更多优化以避免其他奇怪的字符设置,但这似乎足以回答您感兴趣的问题
Don't forget the ^and $like I first did :(
不要忘记^和$我第一次做的一样:(
Also forgot to slash the .- silly me
还忘记砍了.- 傻我
回答by YakovL
To answer the question in the title, I'd update the RegExp by Junuxx and allow dots in the beginning and end of the string:
要回答标题中的问题,我将通过 Junuxx 更新 RegExp 并允许在字符串的开头和结尾使用点:
'/^\.?([^\.]|([^\.]\.))*$/'
which is optional .in the beginning followed by any number of non-.or [non-.followed by .].
它.在开头是可选的,后跟任意数量的非.或 [非.后跟.]。

