正则表达式排除如果前面的字符串?

时间:2015-01-18 01:21:16

标签: c# regex

我之前没有使用过正则表达式,但在网上找到了一些有用的东西:

private string ConvertUrlsToLinks(string msg)
{
    string regex = @"((www\.|(http|https|ftp|news|file)+\:\/\/)[&#95;.a-z0-9-]+\.[a-z0-9\/&#95;:@=.+?,##%&~-]*[^.|\'|\\||\# |!|\(|?|\[|,| |>|<|;|\)])";
    Regex r = new Regex(regex, RegexOptions.IgnoreCase);
    return r.Replace(msg, "<a href=\"$1\" title=\"Click to open in a new window or tab\" target=\"&#95;blank\">$1</a>").Replace("href=\"www", "href=\"http://www").Replace(@"\r\n", "<br />").Replace(@"\n", "<br />").Replace(@"\r", "<br />");
}

它做得很好但现在我希望它排除已经有一个&#34; a href =&#34;在前。结尾了#34; / a&#34;也要考虑。

可以用正则表达式完成,还是必须使用完全不同的方法,比如编码?

3 个答案:

答案 0 :(得分:0)

试试这个:

((?<!href=')(?<!href=")(www\.|(http|https|ftp|news|file)+\:\/\/)[&#95;.a-z0-9-]+\.[a-z0-9\/&#95;:@=.+?,##%&~-]*[^.|\'|\\||\# |!|\(|?|\[|,| |>|<|;|\)])

我在regex101.com上测试了

使用以下样本集:

www.google.com
http://hi.com
http://www.fishy.com
href='www.ignore.com'
www.ouch.com

答案 1 :(得分:0)

使用现有的正则表达式模式,您可以进行一些简单的更改,以处理预先添加或附加到字符串的其他文本:

`.+` <- pattern -> `(.+)?`

哪会给你:

.+((www\.|(http|https|ftp|news|file)+\:\/\/)[&#95;.a-z0-9-]+\.[a-z0-9\/&#95;:@=.+?,##%&~-]*[^.|\'|\\||\# |!|\(|?|\[|,| |>|<|;|\)])(.+)?

所以传递字符串:

<a href='http://www.test.com'>http://www.test.com</a>

...或...

http://www.test.com

会导致:

<a href="http://www.test.com" title="Click to open in a new window or tab" target="&#95;blank">www.test.com</a>

示例:

https://regex101.com/r/bO0cW6/1

http://ideone.com/suVw3I

答案 2 :(得分:0)

我认为毕竟在正则表达式中做到这一点有点小,所以编写了代码,以防有人对此感兴趣:

private string handleatag(string msg, string tagbegin, string tagend)
{
    ArrayList tags = new ArrayList();
    int tagbeginpos = msg.IndexOf(tagbegin);
    int tagendpos;

    string hash = tagbegin.GetHashCode().ToString();

    while (tagbeginpos != -1)
    {
        tagendpos = msg.IndexOf(tagend, tagbeginpos);

        if (tagendpos != -1)
        {
            string atag = msg.Substring(tagbeginpos, tagendpos - tagbeginpos + tagend.Length);
            msg = msg.Replace(atag, hash + tags.Count.ToString());
            tags.Add(atag);
        }
        else
            msg = msg.Remove(tagbeginpos, tagbegin.Length);

        tagbeginpos = msg.IndexOf(tagbegin, tagbeginpos);
    }

    msg = ConvertUrlsToLinks(msg);

    for (int i = 0; i < tags.Count; i++)
        msg = msg.Replace(hash + i.ToString(), tags[i].ToString());

    return msg;
}

private string ConvertUrlsToLinks(string msg)
{
    if (msg.IndexOf("<a href=") != -1)
        return handleatag(msg, "<a href=", "</a>");

    string regex = @"((www\.|(http|https|ftp|news|file)+\:\/\/)[&#95;.a-z0-9-]+\.[a-z0-9\/&#95;:@=.+?,##%&~-]*[^.|\'|\\||\# |!|\(|?|\[|,| |>|<|;|\)])";
    Regex r = new Regex(regex, RegexOptions.IgnoreCase);
    return r.Replace(msg, "<a href=\"$1\" title=\"Click to open in a new window or tab\" target=\"&#95;blank\">$1</a>").Replace("href=\"www", "href=\"http://www").Replace(@"\r\n", "<br />").Replace(@"\n", "<br />").Replace(@"\r", "<br />");
}