我正在尝试验证必须符合以下规则的字符串:
这是我走了多远:
if (preg_match("/^[\p{L}\p{M}][\s\p{L}\p{M}-'\.]{4,}$/u", $name, $matches)) {
echo "Match was found: '{$matches[0]}' in '$name'<br />";
}
我在编写最小4个字母数字字符时遇到困难,其中有一个空格。
我想要匹配一个实体的全名,但是有一些放松的条件。
"ábc é" --> good
"á bcd" --> good
"abc déf" --> good
"ab cd éf" --> good
"a-1 b4." --> good
"a 123--" --> good
"a 12'34 .-56" --> good
"á" --> bad less than 4 alphanumeric
"ab" --> bad less than 4 alphanumeric
"ábc" --> bad less than 4 alphanumeric
"abcd" --> bad no white space in the string
"1ábc d" --> bad starts with a non letter
"-ábc d" --> bad starts with a non letter
".1ábc d" --> bad starts with a non letter
答案 0 :(得分:1)
这可能有用,但没有测试过它 编辑:嗯,经过测试/调试,这就是我得到的,祝你好运!
# ^(?=[\pL\pN\s'\-.]+$)(?=[^\pL\pN]*(?:[\pL\pN][^\pL\pN]*){4,}$)(?![\pN'\-.])(?=\S+\s+\S)
^
(?= [\pL\pN\s'\-.]+ $ ) # Allowed characters
(?= # At least 4 alphanumeric chars
[^\pL\pN]*
(?: [\pL\pN] [^\pL\pN]* ){4,}
$
)
(?! [\pN'\-.] ) # Cannot start with these
(?= # At least 1 whitespace after first char / before last char
\S+ \s+ \S
)