I'm new to regular expressions in Java, and I need to check if the string has alphanumeric characters , commas , apostrophes, and full stops (periods only).
I suggest you use the \p{Alnum} class to match alphanumeric characters:
Pattern p = Pattern.compile("[\\p{Alnum},.']*");
(I noticed that you included \s in your current pattern. If you want to allow white space, just add \s to the character class.)
From the Pattern documentation :
[...]
\p{Alnum} Alphanumeric character: [\p{Alpha}\p{Digit}]
[...]
You do not need to include ^ and {1, ...} . Just use methods like Matcher.matches or String.matches to match the full pattern.
Also note that you do not need to exit . inside a character class ( [ ... ] ).
source share