如何用正则表达式替换所有字符串,并将其自身与另一个字符串连接?

azr*_*n92 0 c# regex

例如,我的正则表达式找到了字符串:some\file\path.xml我希望它被更改为new_root\some\file\path.xml.有没有办法使用正则表达式替换方法?如果没有,那么首选方法是什么?

Tim*_*m B 5

您似乎可以使用Regex.Replace执行您所要求的操作.查看MSDN上正则表达式中的替换文章.

例:

var path = @"C:\some\file\path.xml";
var result = Regex.Replace(path, @"(C:\\)(.*)", "$1new_root\\$2");
Run Code Online (Sandbox Code Playgroud)

结果是C:\new_root\some\file\path.xml.