惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Stack Overflow Blog
Stack Overflow Blog
云风的 BLOG
云风的 BLOG
G
Google Developers Blog
J
Java Code Geeks
C
Check Point Blog
Last Week in AI
Last Week in AI
Microsoft Azure Blog
Microsoft Azure Blog
Blog — PlanetScale
Blog — PlanetScale
月光博客
月光博客
Vercel News
Vercel News
The GitHub Blog
The GitHub Blog
L
LangChain Blog
有赞技术团队
有赞技术团队
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 司徒正美
IT之家
IT之家
Martin Fowler
Martin Fowler
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Tailwind CSS Blog
U
Unit 42
Jina AI
Jina AI
Microsoft Security Blog
Microsoft Security Blog
I
InfoQ

博客园 - 文's sky

泛型列表(List)的搜索和排序 [笔记]读取含日期格式的记录SQLite报错"该字符串未被识别为有效的 DateTime"的问题 FtpWebRequest 的杂症 - "The server committed a protocol violation","基础连接已经关闭: 服务器提交了协议冲突" SqlServer 2000默认开启连接池 设计模式学习系列-C#的单件模式 ASP 整合ASP.NET的URL参数编码问题 - 文's sky 序列化遇到"不应有的节点" 一、NVelocity介绍 NVelocity开篇 准确计算时间差 EXPLORER自动连接221.5.250.163 ASP.NET C# 验证码 支持中文 噪点 弯曲 模板机制的第一个程序Hello World [笔记]DataView格式化显示数据不能的问题 .Net论坛,让人看笑话~ 实在受不了了,大家来帮帮我如何调用这个C++/CLI的DLL C#外挂专用截取显示器图像代码 C#游戏外挂代码 重置递增号
转换链接的正则
文's sky · 2007-12-21 · via 博客园 - 文's sky

转自http://www.cnblogs.com/dudu/archive/2007/12/19/1006119.html,收藏一下.

    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, "<[^>]*>",string.Empty, RegexOptions.Compiled);//清除html标记
        matchs = Regex.Matches(clearText, pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
        
bool flag = true;
        
foreach (Match m in matchs)
        
{
            
string link = "<a href=\"" + m.ToString() + "\" target=\"_new\">" + m.ToString() + "</a>";
            
if (linkMatchs.Count > 0)
            
{
                
foreach (Match linkMatch in linkMatchs)
                
{
                    
if (linkMatch.Value.IndexOf(m.Value) > -1)
                    
{
                        flag 
= false;
                        
break;
                    }

                }

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

            
        }

        
return text;

    }