views:

75

answers:

1

Due to limitations i need to write ONE regex and i would like to blacklist instead of whitelist certain strings (or sites)

Here i want to disallow youtube and IP address. Below is code that matches the two site. How can i NOT match them and allow the two google (or any other site) to match?

Here is some C# code i used to test my regex.

        var mq = Regex.Match("http://google.com\nhttp://youtube.com\nhttp://google.com/ddhf\n72.72.72.72\n",
            @"(.*youtube\.com.*|\d+\.\d+\.\d+\.\d+)", RegexOptions.Multiline);
        while (mq.Success)
        {
            string sz = mq.Groups[0].Value;
            Console.WriteLine(sz);
            mq = mq.NextMatch();
        }

        return;
+2  A: 

You need the ?! construct. So it would be:

(?!.*youtube\.com.*|\d+\.\d+\.\d+\.\d+)
Konamiman
I looked at it, i couldnt make ?! work for my test above
acidzombie24
(?!) is just a "look ahead", meaning, it catches nothing. You have to repeat the pattern, like this: `(?!youtube)([a-z]+)`, then the second parentheses catch any word that is not "youtube".
Boldewyn
output is many blank lines.
acidzombie24