Verify if String is hexadecimal

Horrible abuse of exceptions. Don’t ever do this! (It’s not me, it’s Josh Bloch’s Effective Java). Anyway, I suggest

private static final Pattern HEXADECIMAL_PATTERN = Pattern.compile("\\p{XDigit}+");

private boolean isHexadecimal(String input) {
    final Matcher matcher = HEXADECIMAL_PATTERN.matcher(input);
    return matcher.matches();
}

Leave a Comment