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...
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();
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\-\_\. ]+)");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With