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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测