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


Match a regular expression for a Unicode string with at least one space

我正在尝试验证必须符合以下规则的字符串:

  1. 允许的字符包括:
    • 所有 Unicode 字母 [a-z][A-Z] 和其他字母,如 (á, é, í, ó, ú, ü, ñ, etc...)
    • 所有数字 [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

这可能有效,但尚未对其进行测试。
编辑:好吧,经过测试/调试,这就是我得到的,祝你好运!

 # ^(?=['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 
 )