Regex ignore youtube

a small problem with regex, simple as a pie for those who know, impossible for those who don't :)

I have little experience with regex, so I need your help. I have a pattern that matches urls

[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/] 

this matches all url. I just need to change this regex to match all urls. EXCLUDES the URL "youtube.com".

 http://www.google.com MATCH http://example.com MATCH http://asdasdasfsadfsdfasfsdfs.com MATCH http://www.youtube.com MUST NOT MATCH http://youtube.com MUST NOT MATCH 

(I think you understand)

Thanks in advance guys!

+4
source share
3 answers

You can use a negative lookahead like:

 ^(?!http:\/\/(?:www\.)?youtube\.com).*$ 

Take a look

+2
source

I suggest a small modification to the codaddict answer:

 ^(?!http:\/\/(?:.*\.)*youtube\.com).*$ 

It will also undo any other youtube.com subdomains.

+2
source

Change the regex to

[[:alpha:]]+://(?!(?:[^:/ ]+\.)?youtube.com)[^<>[:space:]]+[[:alnum:]/]

Test here: http://rubular.com/r/Am4YFcCsf9

The key part is [^:/ ]

Carrot says it can't be between // and youtube.com. You can change it to prevent the [^:/ \t] tab, or have it allow the use of certain characters. - the numbers of the underscore letters [\.\-A-Za-z0-9] (also works [\w\.\-] )

Edit: after reading your comment

Now my suggestion

[[:alpha:]]+://(?!(?:https?:/+)*(?:[^:/ ]+\.)?youtube.com)[^<>[:space:]]+[[:alnum:]/]

+1
source

All Articles