Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if a string has only alphanumeric characters in guava?

Tags:

java

guava

Actually, I would like to check if it has only alphanumeric plus these: .-_ [space]

If using an external library I would like to use Guava since it's already included in my project...

like image 385
Moshe Shaham Avatar asked Dec 20 '22 10:12

Moshe Shaham


2 Answers

Regexp-free and somewhat more readable (?) solution could be using Guava's CharMatcher class:

boolean matched = CharMatcher.JAVA_LETTER_OR_DIGIT.or(CharMatcher.WHITESPACE)
    .or(CharMatcher.anyOf("_.-")).matchesAllOf(yourString);

Maybe ASCII chars are OK for your use case? If so, use:

boolean matched = CharMatcher.ASCII.matchesAllOf(yourString);

See wiki page for examples and description.

Also, you may want to extract your matcher to constant and precompute it:

private static final CharMatcher CHAR_MATCHER = CharMatcher.JAVA_LETTER_OR_DIGIT
    .or(CharMatcher.WHITESPACE)
    .or(CharMatcher.anyOf("_.-"))
    .precomputed();

What's more interesting, if you read CharMatcher's documentation you may find that "digit", "letter" and "whitespace" in Java are quite ambigious terms:

Determines whether a character is a digit according to Java's definition. If you only care to match ASCII digits, you can use inRange('0', '9').

or

Determines whether a character is a letter according to Java's definition. If you only care to match letters of the Latin alphabet, you can use inRange('a', 'z').or(inRange('A', 'Z')).

so you may want use explicit char ranges:

private static final CharMatcher CHAR_MATCHER_ASCII = 
    CharMatcher.inRange('0', '9')
        .or(CharMatcher.inRange('a', 'z'))
        .or(CharMatcher.inRange('A', 'Z'))
        .or(CharMatcher.anyOf(" _.-")) // note space here
        .precomputed();
like image 120
Xaerxess Avatar answered May 11 '23 02:05

Xaerxess


Actually you don't need any library. You can do it with regex with plain Java.

Regex: ([A-Za-z0-9\-\_\. ]+)

myStr.matches("([A-Za-z0-9\-\_\. ]+)");
like image 44
Cem Özer Avatar answered May 11 '23 01:05

Cem Özer