日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

将地址转换为链接的正则表达式(regex url href)

發布時間:2025/7/14 59 豆豆
生活随笔 收集整理的這篇文章主要介紹了 将地址转换为链接的正则表达式(regex url href) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
將文章內容中沒有鏈接的地址轉換為鏈接。
代碼如下:
????public?static?string?ShowUrls(string?text)
????
{
????????
//代碼來自博客園?http://www.cnblogs.com
????????Regex?linkRegex?=?new?Regex("?href\\s*=\\s*(?:(?:\\\"(?<url>[^\\\"]*)\\\")|(?<url>[^\\s]*))",
????????????RegexOptions.IgnoreCase?|?RegexOptions.Compiled);????????
????????MatchCollection?linkMatchs?
=?linkRegex.Matches(text);
????????
????????
string?pattern?=?@"(http|ftp|https):\/\/[\w]+(.[\w]+)([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])";
????????MatchCollection?matchs;

????????
string?clearText?=?Regex.Replace(text,?"(<br>|<br/>)","?",?RegexOptions.IgnoreCase);
????????clearText?
=?Regex.Replace(clearText,?"<[^>]*>",?string.Empty,?RegexOptions.Compiled);//清除html標記
????????matchs?=?Regex.Matches(clearText,?pattern,?RegexOptions.IgnoreCase?|?RegexOptions.Compiled);
????????
bool?flag1?=?true;
????????
????????
for(int?i=0;i<matchs.Count;i++)
????????
{
????????????Match?m?
=?matchs[i];
????????????
string?link?=?"<a?href=\""?+?m.ToString()?+?"\"?target=\"_blank\">"?+?m.ToString()?+?"</a>";
????????????
if?(linkMatchs.Count?>?0)
????????????
{
????????????????
foreach?(Match?linkMatch?in?linkMatchs)
????????????????
{
????????????????????
if?(linkMatch.Value.IndexOf(m.Value)?>?-1)
????????????????????
{
????????????????????????flag1?
=?false;
????????????????????????
break;
????????????????????}

????????????????}

????????????}
????????????
????????????
if(flag1)
????????????
{
????????????????
bool?flag2?=?true;
????????????????
for?(int?j?=?0;?j?<?i;?j++)
????????????????
{
????????????????????
if?(m.ToString()?==?matchs[j].ToString())
????????????????????
{
????????????????????????flag2?
=?false;
????????????????????}

????????????????}

????????????????
if?(flag2)
????????????????
{
????????????????????text?
=?text.Replace(m.ToString(),?link);
????????????????}

????????????}

????????????
????????}

????????
return?text;
????}

總結

以上是生活随笔為你收集整理的将地址转换为链接的正则表达式(regex url href)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。