VJS February 2016

java string match pattern with a-z , A-Z, 0-9 and some special char

I need to write a method which match a-z, A-Z , 0-9 and some special charecter like . , @, _, -.

I have written but confused with spcl characters.

public static boolean isAlfaNumeric(String src) {
    if (src == null) {
        return false;
    }
    if (src.matches("[a-zA-z0-9]*")) {
        return true;
    }
    return false;
}

Answers


vjn_amal February 2016

Try this one -

public static boolean isAlfaNumeric(String src) {
    if (src == null) {
        return false;
    }
    if (src.matches("[\\w@,-]*")) {
        return true;
    }
    return false;
}

Put the required special characters inside the square brackets. For your info - \w covers all the word characters which includes alphabets in both cases, digits and underscore character. Hope this is helpful.

Post Status

Asked in February 2016
Viewed 2,634 times
Voted 11
Answered 1 times

Search




Leave an answer