【问题标题】:Utf8 correct regex for CamelCase (WikiWord) in perlPerl 中 CamelCase (WikiWord) 的 Utf8 正确正则表达式
【发布时间】:2011-09-13 10:53:48
【问题描述】:

这里有一个关于CamelCase regex 的问题。结合tchrist post,我想知道正确的 utf-8 CamelCase 是什么。

从 (brian d foy's) 正则表达式开始:

/
    \b          # start at word boundary
    [A-Z]       # start with upper
    [a-zA-Z]*   # followed by any alpha

    (?:  # non-capturing grouping for alternation precedence
       [a-z][a-zA-Z]*[A-Z]   # next bit is lower, any zero or more, ending with upper
          |                     # or 
       [A-Z][a-zA-Z]*[a-z]   # next bit is upper, any zero or more, ending with lower
    )

    [a-zA-Z]*   # anything that's left
    \b          # end at word 
/x

并修改为:

/
    \b          # start at word boundary
    \p{Uppercase_Letter}     # start with upper
    \p{Alphabetic}*          # followed by any alpha

    (?:  # non-capturing grouping for alternation precedence
       \p{Lowercase_Letter}[a-zA-Z]*\p{Uppercase_Letter}   ### next bit is lower, any zero or more, ending with upper
          |                  # or 
       \p{Uppercase_Letter}[a-zA-Z]*\p{Lowercase_Letter}   ### next bit is upper, any zero or more, ending with lower
    )

    \p{Alphabetic}*          # anything that's left
    \b          # end at word 
/x

标记为“###”的行有问题。

另外,假设数字和下划线等价于小写字母时如何修改正则表达式,所以W2X3是一个有效的CamelCase单词。

更新:(ysth 评论)

接下来,

  • any:意思是“大写或小写或数字或下划线”

正则表达式应该匹配 CamelWord, CaW

  • 以大写字母开头
  • 可选任何
  • 小写字母或数字或下划线
  • 可选任何
  • 大写字母
  • 可选任何

请不要标记为重复,因为它不是。 original question(以及答案)只考虑 ascii。

【问题讨论】:

  • 那是你开始使用的一个非常奇怪的正则表达式;我认为它与更简单的 /\b[A-Z]+[a-z][A-Za-z]*\b/ (仅由字母组成的“单词”,以大写字母开头并包括至少一个小写字母)匹配没有任何不同(更新:我错了,原来的正则表达式至少需要三个字母。)
  • 无论如何,请不要以 ASCII 正则表达式开头;尽可能精确地定义您想要匹配的内容
  • 更新了问题——用(我希望足够)精确的定义
  • Nit:当您指的是 Unicode 时,您会说 UTF-8。 UTF-8 是一种将文本存储为字节的方式,但您的正则表达式显然适用于文本。
  • 那不是我的正则表达式。 j_random_hacker came up with that,虽然我后来用 /x 开关修改了它。

标签: regex perl unicode utf-8 camelcasing


【解决方案1】:

我真的不知道你想做什么,但这应该更接近你最初的意图。不过,我仍然不知道你想用它做什么。

m{
    \b
    \p{Upper}      #  start with uppercase code point (NOT LETTER)

    \w*            #  optional ident chars 

    # note that upper and lower are not related to letters
    (?:  \p{Lower} \w* \p{Upper}
      |  \p{Upper} \w* \p{Lower}
    )

    \w*

    \b
}x

永远不要使用[a-z]。事实上,不要使用\p{Lowercase_Letter}\p{Ll},因为它们与更可取和更正确的\p{Lowercase}\p{Lower} 不同。

请记住,\w 实际上只是一个别名

[\p{Alphabetic}\p{Mark}\p{Decimal_Number}\p{Letter_Number}\p{Connector_Punctuation}]

【讨论】:

  • 为什么LowercaseLower 更受欢迎? (即Ll 没有包含哪些内容?)LowercaseLower(如果有)有什么区别?
  • @ikegami: LowercaseLower 是相同的,是GC=Lowercase_LetterOther_Lowercase=True 的联合。有 201 个代码点要么是❶ Lower 但不是 GC=Ll,要么是❷ Upper 但不是 GC=Lu。其中包括 GC=MnGC=LmGC=NlGC=So 代码点。 抱歉,老实说,我现在还以为这是常识! 运行 unichars -gs '/(?= \P{Ll} ) \p{Lower} /x || / (?= \P{Lu} ) \p{Upper} /x' | ucsort --upper-before-lower | cat -n | less -r 看看我的意思。这些程序在我的unicode toolchest.
  • @tchrist - 指向 unicode 工具集的链接已失效(至少现在如此)。有替代品吗?
猜你喜欢
  • 1970-01-01
  • 2015-02-20
  • 1970-01-01
  • 2023-01-23
  • 2014-02-15
  • 2014-07-18
  • 1970-01-01
  • 2012-07-22
  • 1970-01-01
相关资源
最近更新 更多