匹配至少有一个空格的Unicode字符串的正则表达式

3
我正在尝试验证一个字符串,它必须符合以下规则:
  1. 允许的字符有:
    • 所有Unicode字母[a-z][A-Z]和其他字母,如(á,é,í,ó,ú,ü,ñ等...)
    • 所有数字[0-9]
    • 这些特殊字符只有(空格,撇号,破折号,句点)
  2. 该字符串必须至少有4个字母数字字符。空格、撇号、破折号和句点不计入最小长度。
  3. 该字符串不能以数字、撇号、破折号或句点开头。
  4. 该字符串必须至少有1个空格在其字符之间。请注意,该字符串已修剪前导和尾随空格,因此永远不会有前导或尾随空格。
这是我做到的程度:
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

你所有的“--> good”都违反了规则4。 - user557597
没关系,看到例子了。 - user557597
1个回答

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 
 )

+1 抱歉回复晚了。它像魔法一样运行了 :D 非常感谢。 - Songo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接