[英]PHP preg_match and regex: lowercaps only either with hyphens, numbers, or alphabets, and at least 6 characters?
I want to accept an input with lowercaps only either with hyphens, numbers, or alphabets, and at least 6 characters . 我只接受带有连字符,数字或字母以及至少6个字符的小写字母的输入。 But the regex below accepts Upper case as long as the input is longer than 6 characters.
但是,只要输入长度超过6个字符,下面的正则表达式就接受大写。
$user_name = 'BloggerCathrin';
if(!preg_match('/([a-z0-9]{6}|[a-z0-9\-]{6})/', $user_name))
{
echo 'please use lowercaps only, either with hyphens, numbers, or alphabets, and at least 6 characters.';
}
How can I fix that? 我该如何解决?
将您的字符串以^
开头和$
结尾。
"/^[a-z0-9-]{6,}$/"
If you pattern needs to match the whole string - from beginning to end - you need to wrap it inside ^
and $
: 如果您需要从头到尾匹配整个字符串,则需要将其包装在
^
和$
:
/^([a-z0-9]{6}|[a-z0-9\-]{6})$/
start -´ `- end
From PCRE regex syntax - Meta-characters : 从PCRE regex语法开始-元字符 :
^
- assert start of subject (or line, in multiline mode)^
-断言主题的开始(或行,在多行模式下)
$
- assert end of subject or before a terminating newline (or end of line, in multiline mode)$
-声明主题结尾或在换行符终止之前(或在多行模式下为行尾)
If you have a six character minimum (and you also can compact), your pattern might look like: 如果最小字符数为六个(并且还可以压缩),则模式可能如下所示:
/^[a-z0-9-]{6,}$/
```- minimum: 6, maximum: unspecified (empty)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.