使用regex c#从给定段落中找出url#

时间:2011-01-18 11:21:25

标签: c# regex

任何人都可以帮我找到使用正则表达式##

的段落中的url

注意:段落可能还包含字符串和网址

Input like this 
"My website   http://www.google.com/myMail/1212  this is my favorite website"


out put looks:
My website <a href='http://www.google.com/myMail/1212'>http://www.google.com/myMail/1212</a> this is my favorite website

3 个答案:

答案 0 :(得分:0)

here被盗:

public string[] ExtractURLs(string str)
{
    // match.Groups["name"].Value - URL Name
    // match.Groups["url"].Value - URI
    string RegexPattern = @"<a.*?href=[""'](?<url>.*?)[""'].*?>(?<name>.*?)</a>"

    // Find matches.
    System.Text.RegularExpressions.MatchCollection matches
        = System.Text.RegularExpressions.Regex.Matches(str, RegexPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase);

    string[] MatchList = new string[matches.Count];

    // Report on each match.
    int c = 0;
    foreach (System.Text.RegularExpressions.Match match in matches)
    {
        MatchList[c] = match.Groups["url"].Value;
        c++;
    }

    return MatchList;
}

谷歌搜索真的可以让你知道

答案 1 :(得分:0)

直接从RegexBuddy

resultString = Regex.Replace(subjectString, @"\b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[A-Z0-9+&@#/%=~_|]", "<a href=\"$0\">$0</a>", RegexOptions.IgnoreCase);

请注意,该协议是强制性的。

答案 2 :(得分:0)

谢谢M.edmondson先生

谷歌搜索帮助了我,得到了以下答案

string FindUrl(string url)
{
    Regex r1= new Regex("((http://|www\\.)([A-Z0-9.-:]{1,})\\.[0-9A-Z?;~&#=\\-_\\./]{2,})",    RegexOptions.Compiled | RegexOptions.IgnoreCase);

    MatchCollection mc = r1.Matches(url);
    foreach (Match m in mc)
    {
        url = url.Replace(m.Value, "<a href='" + m.Value + "'>" + m.Value + "</a>");
    }
    return url;
}