I am trying to write a String validation to match any character (regular, digit and special) except =.
Here is what I have written -
String patternString = "[[^=][\\w\\s\\W]]*";
Pattern p = Pattern.compile(patternString);
Matcher m = p.matcher(str);
if(m.matches())
System.out.println("matches");
else
System.out.println("does not");
But, it matches the input string "2009-09/09 12:23:12.5=" with the pattern.
How can I exclude = (or any other character, for that matter) from the pattern string?
If the only prohibited character is the equals sign, something like [^=]*
should work.
[^...]
is a negated character class; it matches a single character which is any character except one from the list between the square brackets. *
repeats the expression zero or more times.
First of all, you don't need a regexp. Simply call contains
:
if(str.contains("="))
System.out.println("does not");
else
System.out.println("matches");
The correct regexp you're looking for is just
String patternString = "[^=]*";
如果您只想检查是否出现“=”,为什么不使用String indexOf()方法?
if str.indexOf('=') //...
如果您的目标是在字符串中没有任何=字符,请尝试以下操作
String patternString = "[^=]*";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.