我有一个带值的字符串。我想检查该字符串中是否有返回列表模式。
例如:String myString="a/b[c=\"1\"=\"1\"]/c\^]
我想检查是否有以下模式
- "1"="1"
- ^
我正在使用下面的代码,它总是给出 false
String text = "\"1\"=\"1\" ^ for occurrences of the http:// pattern.";
String patternString = "\"1\"=\"1\"|^";
Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(text);
boolean matches = matcher.matches();
System.out.println("matches = " + matches)
如何用一行正则表达式检查它。
最佳答案
您的代码有几个问题:
String patternString = "\"1\"=\"1\"|^";
这里 ^
必须转义,因为 ^
是一个特殊的元字符,所以要这样写:
String patternString = "\"1\"=\"1\"|\\^";
然后调用:
boolean matches = matcher.matches();
应该改为:
boolean matches = matcher.find();
as matches
尝试匹配完整的输入字符串。
关于java - 使用多个正则表达式检查字符串中是否存在列入黑名单的模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30384745/