简体   繁体   English

PHP的简单PCRE正则表达式只允许一个点或没有?

[英]PHP Simple PCRE regex to only allow 1 dot or none?

I'm trying to create a regex for alias validation: 我正在尝试创建用于别名验证的正则表达式:

And I'm allowing letters, numbers and 1 dot. 我允许使用字母,数字和1个点。

I have done the following: 我已经完成以下工作:

/^[a-z0-9\\.]+$/i

However it allows more then 1 dot? 但是,它允许多于1点吗?

This should do it: 应该这样做:

/^(?:\.[a-z0-9]+|[a-z0-9]+(?:\.[a-z0-9]*)?)$/i

This allows the string to either: 这使字符串可以:

  • start with one dot that is followed by at least one alphanumeric character, or 以一个点开头,后接至少一个字母数字字符,或者
  • start with one or more alphanumeric character that may be followed by one dot and zero or more alphanumeric characters. 以一个或多个字母数字字符开头,后跟一个点和零个或多个字母数字字符。

try this: 尝试这个:

^(?:[a-z0-9]+\.?[a-z0-9]*|[a-z0-9]*\.?[a-z0-9]+)$

places the dot in the center, then allows it to be surrounded on either side. 将圆点放在中心,然后将其围绕在任一侧。

我认为在这种情况下,允许将点作为第一个或最后一个字符不是一个好主意:

/^[a-z0-9]+\.?[a-z0-9]+$/i

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM