1

Possible Duplicate:
Regular expression for excluding special characters

How would I check if a String contains a symbol? let's say I have this String

"SUGAR :::: SUGAR"

I would want to check if that string contains = the following Symbols

":,?,!@#$%^&*()";

I tried this

         Pattern p = Pattern.compile("[?,!,@,$,%,^,&,*,(,)]");
         Matcher m = p.matcher("?");
         boolean b = m.matches();
         System.out.println(b);

But what if the text contains multiple occurrences of that symbol

Community
  • 1
  • 1
KyelJmD
  • 4,682
  • 9
  • 54
  • 77

3 Answers3

1

Try this:

Pattern pat = Pattern.compile("[:?!@#$%^&*()]");
String  str = "SUGAR :::: SUGAR";
Matcher m = pat.matcher(str);
if (m.find()) {
    System.out.println("string contains pattern");
}

The above will check if any part of the string contains at least one occurrence of any of the symbols in the pattern (no need to separate them with ,).

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • that is what I did. few minutes ago. but what if it contains multiple occurrences? – KyelJmD Aug 25 '12 at 15:33
  • 1
    You want to count them or what? Please be a bit more specific, on what you're trying to achieve. – Máthé Endre-Botond Aug 25 '12 at 15:35
  • what is the use of ',' ? – KyelJmD Aug 25 '12 at 15:36
  • @KyelJmD what do you mean? the pattern will match anyway, it doesn't matter if it contains one or more occurrences, as long as it contains at least one – Óscar López Aug 25 '12 at 15:37
  • @KyelJmD the pattern contains a `,` because it was in the original question, I assumed it's one of the symbols to be matched. If that is not the case, you can remove it. – Óscar López Aug 25 '12 at 15:38
  • I was thinking that "," is a separator. – KyelJmD Aug 25 '12 at 15:39
  • @KyelJmD no, `,` is not a separator (there is no need for one!) inside a character class (the characters surrounded by `[...]` in a regular expression). I updated my answer removing the `,` as is not needed. – Óscar López Aug 25 '12 at 15:53
  • @ÓscarLópez I tried to type this "::" but it it didn't return true.I thought if it see one occurrences in the pattern it would return false but how come i returned true? – KyelJmD Aug 27 '12 at 05:32
  • So the caret character(^) does not count as negation if not at the start of a regex, right? The string "$#@" would match a pattern like [:?!@#$%^&*()] but would not match a pattern like [^:?!@#$%&*()]. – Georgy90 Mar 03 '22 at 23:00
1

guava may be?

 CharMatcher matcher = CharMatcher.anyOf(":,?,!@#$%^&*()");
 boolean result = matcher.matchesAnyOf("SUGAR :: SUGAR");
 System.out.println(result);
Eugene
  • 117,005
  • 15
  • 201
  • 306
0

Just simply match against a set containing these characters: [:,?!@#$%^&*()]

If you want to check against the symbol -, position it to the end of the set: [...-]

There's no need to separate elements in a set by comma.

If you want to check for ANY non alphanumeric use \W instead: [\W_] (because \W does not match _)

Máthé Endre-Botond
  • 4,826
  • 2
  • 29
  • 48