将地址转换为链接的正则表达式(regex url href)
生活随笔
收集整理的這篇文章主要介紹了
将地址转换为链接的正则表达式(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\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])";
????????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;
????}
代碼如下:
????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\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])";
????????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)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaScript的9个陷阱及评点
- 下一篇: 在VC++中创建DLL文件并加载