除了描述我想要的(很难解释)之外,让我提供一个示例,说明我需要使用正则表达式在C#中完成的工作:
"HelloWorld" should be transformed to "Hello World"
"HelloWORld" should be transformed to "Hello WO Rld" //Two consecutive letters in capital should be treatead as one word
"helloworld" should be transformed to "helloworld"
编辑:
"HellOWORLd" should be transformed to "Hell OW OR Ld"
每2个连续的大写字母应视为一个单词。
这可能吗?
最佳答案
这是完全可以运行的C#代码,而不仅仅是正则表达式:
Console.WriteLine(
Regex.Replace(
"HelloWORld",
"(?<!^)(?<wordstart>[A-Z]{1,2})",
" ${wordstart}", RegexOptions.Compiled));
它打印:
Hello WO Rld
更新资料
若要使此更加了解UNICODE /国际,请考虑将
[A-Z]
替换为\p{Lt}
(表示用大写字母表示UNICODE的代码点)。当前输入的结果将相同。因此,这是一个更具说服力的示例:Console.WriteLine(Regex.Replace(
@"ÉclaireürfØÑJßå",
@"(?<!^)(?<wordstart>\p{Lu}{1,2})",
@" ${wordstart}",
RegexOptions.Compiled));