Java – Regular expression with URL Encoded Strings

javapattern-matchingregex

I have strings that contain URL encoding (%22) and other characters [!@#$%^&*]. I need to use a RegEx to check if the string contains a character within that group but exclude the URL encoded quote (%22). I can't get the negative look ahead to work properly nor am I able to get an excluded string (or negation) working either. Can someone help? Here is code so far that doesn't work:

Pattern p = Pattern.compile("[!@#$%^&*]"); //
String[] tokens = {"%22Hobo%22", "Shoe*", "Rail%6Road","Sugar"};
for (String string : tokens) {
  Matcher m = p.matcher(string);
  boolean b = m.find()
  System.out.println(string + ": " + b);
}

The desired output should be false, true, true, false.

Best Solution

(?!%22)[!@#$%^&*]

Try this.See demo.

https://regex101.com/r/mS3tQ7/16