开发者

Regex to ignore youtube

开发者 https://www.devze.com 2023-01-29 06:01 出处:网络
a little regex problem, easy as pie for the ones who know, impossible for the ones who doesn\'t :) I have little experience with regex so i need your help on this. i have a pattern that matches URLs

a little regex problem, easy as pie for the ones who know, impossible for the ones who doesn't :)

I have little experience with regex so i need your help on this. i have a pattern that matches URLs

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

this matches all URL's i just need to change this regex so it matches all URL's EXCEPT the "youtube.com" URL's.

http://www.google.com MATCH
http://example.com MATCH
http://asdasdasfsadfsdfasfsdfs.com MATCH
http://www.youtube.com MUST NOT MATCH
http:/开发者_StackOverflow/youtube.com MUST NOT MATCH

(i think you got the point)

Thank you in advance guys!


You can use negative lookahead as:

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

See it


I propose a slight modification of codaddict's answer:

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

This would also discard any other subdomains of youtube.com.


Change your regex to

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

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

The key part is this [^:/ ]

The carrot says what there may not be between // and youtube.com. You may want to change it to not allow tabs [^:/ \t] or to make it to only allow certain characters like . - letters numbers underscores [\.\-A-Za-z0-9] (also works [\w\.\-])

Edit: after reading your comment

My suggestion is now

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号