Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java正则表达式:如果字符串不包含某些顶级域的列表,如何匹配该字符串_Java_Regex - Fatal编程技术网

Java正则表达式:如果字符串不包含某些顶级域的列表,如何匹配该字符串

Java正则表达式:如果字符串不包含某些顶级域的列表,如何匹配该字符串,java,regex,Java,Regex,我试图建立一个正则表达式,在这里我尝试过滤URL,只有那些没有在我的正则表达式中给出的才会导致匹配 如果url中没有test1.com或test2.com,则会导致匹配 我不想导致匹配的顶级域(test1.com和test2.com)始终使用https协议,可以包含子域,并且在顶级域“.com”之后有路径 最后一次尝试如下,但仍然无效 https?://([a-z0-9]+[.])((test1)|(test2))[.](com)/.* regexplanet上的结果: https://abc

我试图建立一个正则表达式,在这里我尝试过滤URL,只有那些没有在我的正则表达式中给出的才会导致匹配

如果url中没有test1.com或test2.com,则会导致匹配

我不想导致匹配的顶级域(test1.com和test2.com)始终使用https协议,可以包含子域,并且在顶级域“.com”之后有路径

最后一次尝试如下,但仍然无效

https?://([a-z0-9]+[.])((test1)|(test2))[.](com)/.*
regexplanet上的结果:

https://abc.test1.com/test.htm 
==>匹配

www.google.com
https://test2.com/test.html
=>没有匹配项

https://123.test2.com/test.html 
==>匹配

www.google.com
https://test2.com/test.html
=>没有匹配项

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

我需要编写正则表达式,使字符串中没有test1.com和test2.com域的所有内容都能匹配吗?

此模式应该可以工作:

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

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"));
结果:

false
true
false
false

这种模式应该有效:

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

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"));
结果:

false
true
false
false

如果您编写正则表达式以匹配,然后,在java代码中:If(matches){discard it}?无法这样做,因为我需要将此正则表达式字符串提供给另一个正在处理此正则表达式的组件。请尝试
^https?:/(?![^/]*(?:test1\.com | test2\.com))\S+$
对于给定的示例,将不返回任何匹配项…如果您编写正则表达式以匹配,然后,在java代码中:if(matches){discard it}?不能这样做,因为我需要将此正则表达式字符串提供给另一个正在处理此正则表达式的组件。请尝试
^https?:/(?![^/]*(?:test1\.com | test2\.com))\S+$
对给定的示例不返回任何匹配项…完美!太多了…太好了!太多了。。。