Monday, February 28, 2011

Java regex to match words with only Alphanumeric and Punctuation characters

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,}";

3 comments:

  1. interesting. I did not know about the Alnum and Punct keywords..

    thanks for sharing...

    ReplyDelete
  2. http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html

    The link has a "Summary of regular-expression constructs"

    ReplyDelete
    Replies
    1. 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