Nicola Peluchetti
Nicola Peluchetti

Reputation: 76880

Regular expression to check allowed characters not working in Java

I have the following method to check allowed characters:

private boolean checkIfAllCharsAreValid(String test) {
    boolean valid = false;
    if (test.matches("^[a-zA-Z0-9,.;:-_'\\s]+$")) {
        valid = true;
    }
    return valid;
}

but if test has the character - in it the match return false. Do i have to escape the -?

Upvotes: 8

Views: 39725

Answers (3)

aioobe
aioobe

Reputation: 421020

Inside [...] the - symbol is treated specially. (You use it yourself in this special purpose in the beginning of your expression where you have a-z.)

You need to escape the - character

[a-zA-Z0-9,.;:\-_'\s]
              ^

or put it last (or first) in the [...] expression like

[a-zA-Z0-9,.;:_'\s-]
                   ^

Some further notes:

  • Technically speaking all characters are valid in the empty string, so I would change from + to * in your expression.

  • String.matches checks the full string, so the ^ and $ are redundant.

  • Your entire method could be wirtten as

    return test.matches("[a-zA-Z0-9,.;:_'\\s-]*");
    

Upvotes: 16

codaddict
codaddict

Reputation: 455052

A - in a character class surrounded on both sides is a regex meta character to denote range.

To list a literal - in a char class you escape the - in the char class:

if (test.matches("^[a-zA-Z0-9,.;:\\-_'\\s]+$")) 
                                 ^^^ 

or place the - at the end of char class:

if (test.matches("^[a-zA-Z0-9,.;:_'\\s-]+$")) 
                                      ^

or place the - at the beginning of char class:

if (test.matches("^[-a-zA-Z0-9,.;:_'\\s]+$")) 
                    ^

Upvotes: 2

a'r
a'r

Reputation: 36999

You can put the - at the start of the character group so that it is not interpreted as a character range.

Upvotes: 2

Related Questions