我有这个代码用正则表达式分割CamelCase:
Regex.Replace(input, "(?<=[a-z])([A-Z])", " $1", RegexOptions.Compiled).Trim();
但是,它没有正确拆分:ShowXYZColours
它会生成Show XYZColours
而不是Show XYZ Colours
如何获得所需的结果?
答案 0 :(得分:15)
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类别。
答案 1 :(得分:6)
<强> .NET DEMO 强>
您可以使用以下内容:
(?<=[a-z])([A-Z])|(?<=[A-Z])([A-Z][a-z])
代码:
string strRegex = @"(?<=[a-z])([A-Z])|(?<=[A-Z])([A-Z][a-z])";
Regex myRegex = new Regex(strRegex, RegexOptions.None);
string strTargetString = @"ShowXYZColours";
string strReplace = @" $1$2";
return myRegex.Replace(strTargetString, strReplace);
输出:
Show XYZ Colours
<强> Demo and Explanation 强>
答案 2 :(得分:1)
使用Tomalak的正则表达式与.NET System.Text.RegularExpressions在结果数组的位置0创建一个空条目:
Regex.Split("ShowXYZColors", @"(?=\p{Lu}\p{Ll})|(?<=\p{Ll})(?=\p{Lu})")
{string[4]}
[0]: ""
[1]: "Show"
[2]: "XYZ"
[3]: "Colors"
它适用于caMelCase(与PascalCase相反):
Regex.Split("showXYZColors", @"(?=\p{Lu}\p{Ll})|(?<=\p{Ll})(?=\p{Lu})")
{string[3]}
[0]: "show"
[1]: "XYZ"
[2]: "Colors"
答案 3 :(得分:1)
您可以尝试以下操作:
Regex.Replace(input, "((?<!^)([A-Z][a-z]|(?<=[a-z])[A-Z]))", " $1").Trim();
示例:
Regex.Replace("TheCapitalOfTheUAEIsAbuDhabi", "((?<!^)([A-Z][a-z]|(?<=[a-z])[A-Z]))", " $1").Trim();
输出: 阿联酋的首都是阿布扎比