我希望我的字符串不包含 *
、;
和 $
。我用这个代码
private static boolean IsMatch(String s, String pattern) {
try {
Pattern patt = Pattern.compile(pattern);
Matcher matcher = patt.matcher(s);
return matcher.matches();
} catch (RuntimeException e) {
return false;
}
}
String regex ="[^*;$]";
System.out.println(IsMatch(url,regex));
但是这个方法总是返回false。 谁能告诉我这是什么问题
最佳答案
尝试使用 [^*;$]*
作为正则表达式。如果您的字符串不包含任何 *
、;
和 $
,它将返回 true。我假设您希望您的正则表达式匹配不包含任何这些字符的字符串,因为您已经在 [
中使用了 ^
和 ]
。
关于java - 在 Android 上使用正则表达式验证字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5646383/