我有这段代码可以通过正则表达式拆分 CamelCase:
Regex.Replace(input, "(?<=[a-z])([A-Z])", " $1", RegexOptions.Compiled).Trim();
但是,它没有正确拆分:ShowXYZColours
它生成 Show XYZColours
而不是 Show XYZ Colours
如何获得想要的结果?
最佳答案
Unicode 识别
(?=\p{Lu}\p{Ll})|(?<=\p{Ll})(?=\p{Lu})
分割:
(?= # look-ahead: a position followed by... \p{Lu}\p{Ll} # an uppercase and a lowercase ) # | # or (?<= # look-behind: a position after... \p{Ll} # an uppercase ) # (?= # look-ahead: a position followed by... \p{Lu} # a lowercase ) #
与您的正则表达式拆分函数一起使用。
编辑:当然,您可以将 \p{Lu}
替换为 [A-Z]
并将 \p{Ll}
替换为 [a-z]
如果这是您的需要,或者您的正则表达式引擎不理解 Unicode 类别。
关于c# - 用正则表达式拆分 CamelCase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21326963/