菜鸟现在有这样两组字符串
1.
框架间距|左部分宽|220
框架间距|右部分宽|722
2.
框架间距|左部分宽|600
框架间距|右部分宽|100
框架背景|当前位置|images/po.gif
框架间距|当前位置下边距|12
我现在想把 2中红色区域的字符串 与 1中的相同的 后面的配置给替换成 1 里面的配置
最终实现的功能是把 600 100替换成 220 722
哎 正则 不是很会 提供参考 (.*?)\|(.*?)\|(.*?) 在线等 谢了
1.
框架间距|左部分宽|220
框架间距|右部分宽|722
2.
框架间距|左部分宽|600
框架间距|右部分宽|100
框架背景|当前位置|images/po.gif
框架间距|当前位置下边距|12
我现在想把 2中红色区域的字符串 与 1中的相同的 后面的配置给替换成 1 里面的配置
最终实现的功能是把 600 100替换成 220 722
哎 正则 不是很会 提供参考 (.*?)\|(.*?)\|(.*?) 在线等 谢了
首先用全字符串匹配的表达式验证下格式,然后用前半部分匹配的替换内容
如果用正则的话
string regStr = @"(.*)\|(.*)\|200";
System.Text.RegularExpressions.Regex mReg = new Regex(regStr, RegexOptions.IgnoreCase);
string str = this.txt_rs.Text;
Match mMatch = mReg.Match(str); while (mMatch.Success)
{
string temp = mMatch.Value;
string[] arr = temp.Split('|');
string value = temp;
if (arr.Length == 3)
{
arr[2] = "600"; //替换成600,以防止前面是"200|200|200"全部替换
value = arr[0] + "|" + arr[1] + "|" + arr[2]; //或者这里加600
}
str = str.Replace(temp, value);
mMatch = mMatch.NextMatch();
}
this.txt_rs.Text = str;
上面代码中
<text style="1111|200|200">
<text style="1111|200|300"><text style="222|11|300">得到结果
<text style="1111|200|600">
<text style="1111|200|300"><text style="222|11|300">