0

I have a regex which matches all the websites but i want to exclude 2 specific websites from this regex?

Regex is

[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)

Websites I want to exclude are

www.gfycat.com
www.imgur.com
imgur.com/*
gfycat.com/*

Is it possible to write the regex which exludes the specific websites? Any suggestions on how to solve this problem?

/[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/

I have attached the screenshot for match patterns. enter image description here

Using RES and regex need to be implemented here.enter image description here

8
  • What are you trying to achieve in the end? Replace the text with some other text? Or just collect matches? The technique will be the same: match what you do not need and then match and capture what you need. See this regex demo. The captured text is what you need to allow, else, do not. Have you got the access to the code? Commented Jun 20, 2017 at 8:35
  • How are you using this regex? Are you searching for urls or are you validating them? Can I use a start-of-string anchor ^ in the regex? Commented Jun 20, 2017 at 8:36
  • Iam using it in Reddit to filter out posts and hide all other posts except gfycat.com and imgur.com posts by matching the regex.If i use the above mentioned regex all the posts are hidden. Commented Jun 20, 2017 at 8:38
  • 1
    Try regex101.com/r/EwKAbR/2. Commented Jun 20, 2017 at 8:43
  • 1
    @BackdoorCipher: There is no universal solution for this with a plain single regex. You might try enhancing to this one, but there still may be other edge cases. Commented Jun 20, 2017 at 9:04

2 Answers 2

1

Try this

^(?:(?!(?:www\.)?(?:google|gfycat|imgur))[-a-zA-Z0-9@:%._\+~#=]{2,256})\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)
Sign up to request clarification or add additional context in comments.

2 Comments

www.gfycat.com/ABC Fails
@BackdoorCipher Some spelling mistake in pattern. Now you can check.
0

Not sure, why you need regex to do the same. Can you not do something simple like the below, unless I understood it completely wrong.

url = new URL('https://www.google.co.uk/?gfe_rd=cr&ei=bN5IWaP7CYyDtAHKv4CIBg#q=hello');

if url.hostname == 'www.google.com'
// ignore
else
// process 

The answer is not relevant to the specific question as OP is using a different tool

1 Comment

I have an extenstion which have filter module and I will attach the Screenshot here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.