The title says it:
private static final String onlyAlphaNumericAndPunctuationRegex = "[\\p{Alnum}\\p{Punct}]*";
//returns true
"0123!\"#$%&<=~abcijkxyzABC".matches(onlyAlphaNumericAndPunctuationRegex);
//returns false
"hello test space".matches(onlyAlphaNumericAndPunctuationRegex);
//returns false
"some_àèìòù-ÀÈÌÒÙ_more".matches(onlyAlphaNumericAndPunctuationRegex);
if you need to have at least one character change the regex to:
"[\\p{Alnum}\\p{Punct}]{1,}";
interesting. I did not know about the Alnum and Punct keywords..
ReplyDeletethanks for sharing...
http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html
ReplyDeleteThe link has a "Summary of regular-expression constructs"
Thanks! The post is meant for someone who is looking for a quick hit solution to the specific problem, not for someone who wants to find a solution by thoroughly reading the documentation. Btw, java is up to 7 now, so the most current version of the docs would be /7.
Delete