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?
By default, the '. ' dot character in a regular expression matches a single character without regard to what character it is. The matched character can be an alphabet, a number or, any special character.
All the characters other than the English alphabet (both cases) and, digits (0 to 9) are considered as non-word characters. You can match them using the meta character “\W”.
Wildcard which matches any character, except newline (\n). Used to match 0 or more of the previous (e.g. xy*z could correspond to "xz", "xyz", "xyyz", etc.) ?!= or ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With