如何用正则表达式用小写字母替换大写字母?

aro*_*ron 22 c# regex

我需要用小写字母替换变量名中的大写字母并添加空格

例如:

NotImplementedException应该Not implemented exception UnhandledException应该Unhandled exception

Jen*_*ens 39

由于您没有指定语言,我将在C#中给出一个示例.我相信你的语言会提供类似的东西.

String s = "NotImplementedException";
s = Regex.Replace(s, @"\B[A-Z]", m => " " + m.ToString().ToLower());
// s = "Not implemented exception"
Run Code Online (Sandbox Code Playgroud)

  • 这个答案的更好的C#版本,它将支持任何大写字母(甚至是重音字母):`String s ="NotImplementedException"; s = Regex.Replace(s,@"\ B\p {Lu}",m =>""+ m.ToString().ToLower());` (5认同)