我要全部更换分号括在引号用空间。我如何在C#
中做到这一点?正则表达式替换分号之间引号C#
例如:
这个字符串:
this is an example; "this is other ; example"
将返回:
this is an example; "this is other example"
我在等待着您的帮助!
我要全部更换分号括在引号用空间。我如何在C#
中做到这一点?正则表达式替换分号之间引号C#
例如:
这个字符串:
this is an example; "this is other ; example"
将返回:
this is an example; "this is other example"
我在等待着您的帮助!
试试这个:
string input = "this is an example; \"this is other ; example\"";
var regex = new Regex("\\\"(.*?)\\\"");
var output = regex.Replace(input, m => m.Value.Replace(";"," "));
编辑:这将工作。
string yourString = "hello there; \"this should ; be replaced \"";
string fixedString = Regex.Replace(yourString, "(\"[^\",]+);([^\"]+\")", delegate (Match match)
{
string v = match.ToString();
return v.Replace(";", " ");
});
修改用空格代替 – justiceorjustus
对我有用,谢谢! –
这对于字符串不起作用像''这是一个例子; “这是其他”; \“example \”“'因为它会删除第二个分号。 – juharr
是否有你的字符串逃过任何序列?像'\\''为反斜杠和'\''为双引号?如果是CSV,也许一个CSV解析器比用双引号替换';'更合适? http://stackoverflow.com/questions/6542996/how-to-split-csv-whose-columns-may-contain/6543418#6543418)。 –
是的,它是一个.csv文件,我会考虑使用解析器,谢谢 –