简体   繁体   English

正则表达式匹配Url,除了某个域

[英]Regular expression to match Url's, except a certain domain

I have the following regular expression that matches Url's. 我有以下与Url匹配的正则表达式。 What I want to do is make it not match when a URL belongs to a certain domain, let's say google.com. 我想要做的是当URL属于某个域时使其不匹配,让我们说google.com。

How can I do that? 我怎样才能做到这一点? I've been reading other question and regular expression references and so far I could achieve it. 我一直在阅读其他问题和正则表达式引用,到目前为止我可以实现它。 My Regular expression: 我的正则表达式:

^(https?:\/\/)?([\da-zA-Z\.-]+)\.([a-zA-Z\.]{2,6})([\/\w \.-]*)*\/?$

I use this to filter messages in a chat, I'm using C# to do so. 我使用它来过滤聊天中的消息,我正在使用C#这样做。 Here's a tool in case you want to dig further: http://regexr.com/3faji 如果您想进一步挖掘,这里有一个工具: http//regexr.com/3faji

C# extension method: C#扩展方法:

static class String
{
    public static string ClearUrl(string text)
    {
        Regex regx = new Regex(@"^(https?:\/\/)?([\da-zA-Z\.-]+)\.([a-zA-Z\.]{2,6})([\/\w \.-]*)*\/?$",
        RegexOptions.IgnoreCase);
        string output = regx.Replace(text, "*");

        return output;

    }
}

Thanks for any help 谢谢你的帮助

You can use negative lookahead in your regex to avoid matching certain domains: 您可以在正则表达式中使用否定前瞻以避免匹配某些域:

^(https?:\/\/)?(?!(?:www\.)?google\.com)([\da-zA-Z.-]+)\.([a‌​-zA-Z\.]{2,6})([\/\w .-]*)*\/?$

Or else: 要不然:

^(https?:\/\/)?(?!.*google\.com)([\da-zA-Z.-]+)\.([a‌​-zA-Z\.]{2,6})([\/\w .-]*)*\/?$

(?!(?:www\\.)?google\\.com) is negative lookahead that will assert failure when we have www.google.com or google.com ahead. (?!(?:www\\.)?google\\.com)是负面预测,当我们提前www.google.comgoogle.com时会断言失败。

RegEx Demo RegEx演示

这应该使用负向前瞻,并且还包括以www而不是协议开头的URL,也不是行的第一个字符:

((http|ftp|https):\/\/|www.)(?!google|www.google)[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM