dongyong1942 2013-09-18 15:24
浏览 110
已采纳

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

I'm trying to validate a string that must conform to the following rules:

  1. Allowed characters are:
    • All Unicode letters [a-z][A-Z] and other letters such as (á, é, í, ó, ú, ü, ñ, etc...)
    • All numbers [0-9]
    • These special characters only (white spaces, apostrophes, dashes, dots)
  2. The string must have at least 4 alphanumeric characters. White spaces, Apostrophes, Dashes and Dots don't count in the min length.
  3. The string can not start with a Number, Apostrophes, Dashes or Dots.
  4. The string must have at least 1 white space between its characters. Note that the string is trimmed for leading and trailing white spaces, so there will never be leading or trailing spaces.

This is how far I got:

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 />";
}

I'm having difficulty writing the min 4 alphanumeric characters with a space in it condition.

I'm tying to match an entity's full name, but with some relaxed conditions.

Examples

:

"á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
  • 写回答

1条回答 默认 最新

  • dtujfmfs06058 2013-09-18 16:20
    关注

    This might work, but havn't tested it.
    Edit: Well, after testing/debugging, this is what I get, good luck!

     # ^(?=[\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 
     )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图