日期:2014-05-18 浏览次数:20415 次
string inputString = "噢噢噢[url='http://www.cSdN.net']cSdn[/url]哈哈哈[url=http://www1.cSdn.com]aaaCSDnbbb[/url]我是csDn你是csDN吗?"; string strR="Northsnow"; string strR2="$1" + strR + "$2"; string strResult; strResult = Regex.Replace(inputString, @"([url\s*=\s*[\'\""\s]*?[^\""\']*[\'\""]*?\][^\[\]]*?)csdn([^\[\]]*?\[/url])",strR2,RegexOptions.IgnoreCase ); strResult = Regex.Replace(strResult, @"([url\s*=\s*[\'\""\s]*?[^\""\']*?)csdn([^\""\']*?[\'\""]*?\][^\[\]]*?\[/url])", strR2, RegexOptions.IgnoreCase); strResult = Regex.Replace(strResult, @"(csdn)", "[url='http://blog.csdn.net/precipitant']$1[/url]", RegexOptions.IgnoreCase); strResult = strResult.Replace(strR, "csdn"); MessageBox.Show(inputString + System.Environment.NewLine + strResult );
------解决方案--------------------
-_-#表述问题不清楚,这类问题最适合用委托来处理了
借用楼上的例子
string test = "噢噢噢[url='http://www.cSdN.net']cSdn[/url]哈哈哈[url=http://www1.cSdn.com]aaaCSDnbbb[/url]我是csDn你是csDN吗?"; string result = Regex.Replace(test, @"^[\s\S]*?(?=\[url[^\]]*\])|\[/url\][\s\S]*?(?=\[url[^\]]*\])|\[/url\][\s\S]*?$|^[\s\S]*$", myEvaluator, RegexOptions.IgnoreCase); MessageBox.Show(result); private string myEvaluator(Match m) { return Regex.Replace(m.Value, @"csdn", "[url='http://www.csdn.net']csdn[/url]", RegexOptions.IgnoreCase); }