anshulkatta
anshulkatta

Reputation: 2064

wrong matching for string containing '[' at start

My code is below

public static void main(String[] args) {

        System.out.println(validateusername("$asd123"));

    }

    static boolean validateusername(String s)
    {
        if(s.matches("[a-zA-z]+[0-9]*"))
            return true;
        else return false;
    }

This is giving true for ' [abc1 '

' [ ' character should not be treated as a-zA-z , why its giving true.

But for any other special character it is giving false , ' _ '(underscore) and ' [ ' it is giving true;

What I have tried -

-- i tried putting ' ^ ' in start of string but its not working still.

-- i tried to escape ' [ ' character by putting ' / ' , not working.

Upvotes: 2

Views: 56

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174776

Your pattern is [a-zA-z], you mentioned the range A to z inside the character class. [ symbol falls within the range capital A to small z, so it returns true.

See the ASCII table for the symbols which falls within the range A to z .

Upvotes: 4

Related Questions