douqiao5552 2012-09-22 09:16
浏览 43
已采纳

我认为这个正则表达式不应该失败,我错过了什么?

^(?![_\.\'\-])(?:[\p{L} ]+)$

If I understand correctly, there is:

  • (?![_\.\'\-]) a negative lookahead, that is the string cannot start with underscore, point, apostrophe or minus sign (any number of).
  • (?:[\p{L} ]+) allowing at least one character in Ll, Lm, Lo, Lt and Lu and spaces.

First question is: something like "1Bob" should not fail (because of the lookahead). So why it fails?

Second question is where I can find a list or explanation of characters in Ll, Lm, Lo, Lt and Lu?

  • 写回答

2条回答 默认 最新

  • dongshan3806 2012-09-22 09:29
    关注

    The digit "1" is not matched by \p{L} (this matches only letters!). If you want to match any (numeric) digit, use the class \p{N} as well:

    $text = "1Bob";
    
    if (preg_match("/^(?![_\.\'\-])(?:[\p{N}\p{L} ]+)$/u", $text)) {
      echo "Matched!
    ";
    } else {
      echo "No match...
    ";
    }
    

    which will print:

    Matched!
    

    Also, there are small differences between Ruby's regex engine and that of PHP. Since your target language seems to be PHP, I recommend testing it with PHP, not with Rubular (Ruby).

    Note that inside character classes, the "normal" regex meta chars don't have any special powers and need not be escaped: preg_match("/^(?![_.'-])(?:[\p{N}\p{L} ]+)$/u", $text)

    An overview of many Unicode Character Properties/Classes can be found here: http://www.regular-expressions.info/unicode.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比