日期:2014-05-19  浏览次数:20846 次

正则表达式!Help
请问:
        c#下如何使用正则表达式把
        <a   href= "xxx.xxx.com "> xxxx </a>
替换成
        [url=xxx.xxx.com]xxxx[/url]

谢谢赐教!

------解决方案--------------------
这个直接replace就行了
------解决方案--------------------
string str = "............... ";
Label1.Text = Regex.Replace(str, " <a href=\ "(.*)\ "> (.*) </a> ", "[url=$1]$2[/url] ", RegexOptions.IgnoreCase);
------解决方案--------------------
楼上方法可以一试
------解决方案--------------------
string str = " <a href=\ "xxx.xxx.com\ "> xxxx </a> ";
Label1.Text = Regex.Replace(str, " <a href=[\\s\\S\\n]*\ "(.*)\ "> (.*) </a> ", "[url=$1]$2[/url] ", RegexOptions.IgnoreCase);

------解决方案--------------------
改一下
string str = " <a href=\ "xxx.xxx.com\ "> xxxx </a> ";
Label1.Text = Regex.Replace(str, " <a[\\s\\S\\n]*href=\ "(.*)\ "> (.*) </a> ", "[url=$1]$2[/url] ", RegexOptions.IgnoreCase);

------解决方案--------------------
try

string yourStr = ..................;
string resultStr = Regex.Replace(yourStr, @ " <a[^> ]*?href= " "([^ " "]*?) " "[^> ]*?> ([\s\S]*?) </a> ", "[url=$1]$2[/url] ", RegexOptions.IgnoreCase);

------解决方案--------------------
string alink = " <a href=\ "xxx.xxx.com\ "> xxxx </a> ";
//替换成
//[url=xxx.xxx.com]xxxx[/url]
alink = System.Text.RegularExpressions.Regex.Replace(alink, " <a[\\s\\S]*?href=\ "(.*)\ "> (.*) </a> ", "[url=$1]$2[/url] ", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

------解决方案--------------------
另外,如果href=后可能为“ '”或是直接接网址,可以这样

string yourStr = ..................;
string resultStr = Regex.Replace(yourStr, @ " <a[^ <]*?href=([ ' " "])?([^ ' " "\s> ]*)\1?[^> ]*?> ([^ <]*?) </a> ", "[url=$2]$3[/url] ", RegexOptions.IgnoreCase);