user2028856
user2028856

Reputation: 3183

Regular expression selectively match

Suppose there are the following strings:

https://domain1.com
https://domain2.com    
https://domain3.com
https://testdomain.com
https://domain4.com
https://domain5.com
random text
293928382

How can I match only strings that have http but also exclude ones that contain testdomain for example? Currently I have this which excludes testdomain

^((?!testdomain).)*$

However I don't know how to combine the http matching part with the above expression. Can someone please help me combine the above with matching http as well?

The objective is to match:

https://domain1.com
https://domain2.com    
https://domain3.com
https://domain4.com
https://domain5.com

Thanks!

Upvotes: 1

Views: 27

Answers (1)

anubhava
anubhava

Reputation: 784958

You may use this regex with a negative lookaheadd:

^https?://(?!(?:www\.)?testdomain\.).+$

RegEx Demo

RegEx Details:

  • ^https?://: Match http:// or https:// at the start
  • (?!(?:www\.)?testdomain\.): Negative lookahead to assert that we don't have www.testdomain.com or testdomain.com right after
  • .+$: Match 1+ of any character

Upvotes: 1

Related Questions