0

im trying to build a regex where i try to filter urls and only those who are not given in my regex will result in a match.

If there is no test1.com or test2.com in the url it should result in a match.

The top level domains i want not to result in a match (test1.com and test2.com) are using always the https protocol, can contain subdomains and having paths after the top level domain ".com".

Last try was the following but still doesnt work...

https?://([a-z0-9]+[.])((test1)|(test2))[.](com)/.*

Result on regexplanet:

https://abc.test1.com/test.htm 

==> MATCH

www.google.com

==> NO MATCH

https://123.test2.com/test.html 

==> MATCH

https://test2.com/test.html

==> NO MATCH

Ho do i need to write the regex that everything which has not the test1.com and test2.com domain in its string will give a match?

StephanM
  • 1,350
  • 2
  • 24
  • 50

1 Answers1

2

This pattern should work:

^((?!test1\\.com|test2\\.com).)*$

Try out:

System.out.println(Pattern.matches("^((?!test1\\.com|test2\\.com).)*$", "https://abc.test1.com/test.htm"));
System.out.println(Pattern.matches("^((?!test1\\.com|test2\\.com).)*$", "www.google.com"));
System.out.println(Pattern.matches("^((?!test1\\.com|test2\\.com).)*$", "https://123.test2.com/test.html"));
System.out.println(Pattern.matches("^((?!test1\\.com|test2\\.com).)*$", "https://test2.com/test.html"));

Results:

false
true
false
false
aexellent
  • 145
  • 9