I wonder while the following regex returns false. I can’t see while a “|” should not match. Any ideas? And how do I allow the pattern to match “|”?
Pattern pattern = Pattern.compile("([^\\*|\\\\|\\{|\\}|\\[|\\]|=|>|<])*");
boolean valid = pattern.matcher("|").matches();
Thanks in advance.
The
[^ ]expression matches any character not in the brackets. Since|is in the brackets, it is not matched. All characters (except a few, like\and]) are taken literally inside the[^ ]expression.I think you meant to say something like this:
This matches anything but these characters:
To put it in a string, you would do this: