这里有一个关于CamelCase regex的问题。结合tchrist post我想知道什么是正确UTF-8驼峰。在Perl中驼峰(WikiWord)Utf8正确的正则表达式
与(布莱恩·d FOY的)正则表达式开始:
/
\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是一个有效的驼峰字。
更新时间:(YSTH评论)
下一个,
any
:意思是 “大写或小写字母或数字或下划线”
正则表达式应该匹配CamelWord, CaW
- 开始用大写字母
- 可选任何
- 小写字母或数字或下划线
- 可选任何
- 大写字母
- 可选任何
请,不标记为重复,因为它不是。 original question(和答案)只认为ascii。
别名也就是说,你已经开始与一个真正奇怪的正则表达式;我认为它与简单的'/ \ b [AZ] + [az] [A-Za-z] * \ b /'不同,它与任何不同的东西都不相同(一个“单词”仅由字母组成,以大写字母并包括至少一个小写字母)(更新:我错了,原始正则表达式至少需要三个字母。) – ysth 2011-06-12 16:25:14
无论如何,请不要以ASCII正则表达式开头;开始尽可能准确定义你想要匹配什么 – ysth 2011-06-12 16:29:01
更新了问题 - (我希望是足够的)精确定义 – jm666 2011-06-12 17:02:57