Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using regex to match any character except =

Tags:

java

regex

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?

like image 647
conceptSeeker Avatar asked Mar 19 '12 13:03

conceptSeeker


People also ask

What regex matches any character?

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.

Which pattern is used to match any non What 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”.

Which regular expression matches any character except XYZ?

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 ?


1 Answers

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.

like image 179
tripleee Avatar answered Sep 19 '22 19:09

tripleee