源码
private void button4_Click(object sender, EventArgs e)
{
string file = @"C:\Users\ZMY\Desktop\123.txt";
string txt = File.ReadAllText(file);
//string str1 = @"""[A-Z]+"":\s*""([^""]*)""([^""]*)"",";
string str1 = @"""[A-Z]+"":\s*""[^""]*""[^""]*"",";
txt = Regex.Replace(txt, str1, new MatchEvaluator(OutPutMatch), RegexOptions.None);
MessageBox.Show(txt2);
}
private static string OutPutMatch(Match match)
{
string txt = match.Value;
int idx = txt.IndexOf(":");
string str1 = txt.Substring(0, idx + 1);
string str2 = txt.Substring(idx + 1);
string str = txt.Substring(0, idx + 1) + "\"" + str2.Replace("\"", "").Replace(",", "").Trim() + "\",";
return str;
}
123.txt文件内容:
"ZYSPMC": "*纸制品*"K85 ABC亲柔立围量多日用轻透薄棉柔表层卫生巾8片( 含KMS健康配方) ","SPSM ":""
目的:
为了去掉 纸制品* 后边的"。
参考:
C# Regex类用法,MatchEvaluator委托(转载)
正则表达式之零宽断言