我希望使用正则表达式 From string txt = "http://{ifnotmobile:www.abc.com}{ifmobile:m.abc.com}/?creative={ifnotmobile:1234}";
期待以下输出 http://abc.com/?creative=1234
{ifmobile: 应该被它之后的值替换,直到到达下一个“}”。应删除“{Ifnotmobile:”的任何块。请注意,可以有多个这样的块。
我面临的问题是整个字符串“www.abc.com}{ifmobile:m.abc.com}/?creative={ifnotmobile:1234”被匹配为单词。虽然只有 www.abc.com 应该匹配。我无法在“{ifmobile:”之后的第一个“}”处停止匹配。
字符串 txt = "http://{ifnotmobile:www.abc.com}{ifmobile:m.abc.com}/?creative={ifnotmobile:1234}";
Regex ifnotmobileRegex = new Regex("(\\{)(ifnotmobile:)((?:.*))(\\})", RegexOptions.IgnoreCase | RegexOptions.Singleline);
MatchCollection matchColl = ifnotmobileRegex.Matches(txt);
foreach (Match match in matchColl)
{
String c1 = match.Groups[1].ToString();
String c2 = match.Groups[2].ToString();
String word = match.Groups[3].ToString();
String c3 = match.Groups[4].ToString();
String machingPattern = c1 + c2 + word + c3;
txt = txt.Replace(machingPattern, word);
}
Regex ifmobileRegex = new Regex("(\\{)(ifmobile:)((?:.*))(\\})", RegexOptions.IgnoreCase | RegexOptions.Singleline);
MatchCollection matchColl2 = ifmobileRegex.Matches(txt);
foreach (Match match in matchColl2)
{
String c1 = match.Groups[1].ToString();
String c2 = match.Groups[2].ToString();
String word = match.Groups[3].ToString();
String c3 = match.Groups[4].ToString();
String machingPattern = c1 + c2 + word + c3;
txt = txt.Replace(machingPattern, "");
}
Response.Write("<br>");
Response.Write(txt);
谢谢