[英]RegEx replace query to pick out wiki syntax
我有一个HTML字符串,我需要从例如HTML中获取“ [Title | http://www.test.com] ”模式
“ dafasdfasdf,adfasd。[测试| http://www.test.com/] adf ddasfasdf [SDAF | http://www.madee.com/] assg广告”
我需要将“ [Title | http://www.test.com] ”替换为“ http://www.test.com/'>Title”。
解决这个问题的最佳途径是什么?
我越来越接近:
string test = "dafasdfasdf adfasd [Test|http://www.test.com/] adf ddasfasdf [SDAF|http://www.madee.com/] assg ad ";
string p18 = @"(\[.*?|.*?\])";
MatchCollection mc18 = Regex.Matches(test, p18, RegexOptions.Singleline | RegexOptions.IgnoreCase);
foreach (Match m in mc18)
{
string value = m.Groups[1].Value;
string fulltag = value.Substring(value.IndexOf("["), value.Length - value.IndexOf("["));
Console.WriteLine("text=" + fulltag);
}
必须有一种更干净的方法来获取两个值,例如“ Title”位和url本身。
有什么建议么?
替换模式:
\[([^|]+)\|[^]]*]
有:
$1
简短说明:
\[ # match the character '['
( # start capture group 1
[^|]+ # match any character except '|' and repeat it one or more times
) # end capture group 1
\| # match the character '|'
[^]]* # match any character except ']' and repeat it zero or more times
] # match the character ']'
AC#演示看起来像:
string test = "dafasdfasdf adfasd [Test|http://www.test.com/] adf ddasfasdf [SDAF|http://www.madee.com/] assg ad ";
string adjusted = Regex.Replace(test, @"\[([^|]+)\|[^]]*]", "$1");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.