javaregexmatch

Java Regular Expression - Characters "(" and ")"Matches the Pattern "[\\p{Alpha} '-,.]"


I have been wondering why characters "(" and ")" matches the pattern "[\\p{Alpha} '-,.]".
Please take a look at my code. The method matches will return true for the characters "(" and ")".
Also, are there any other characters that will return true if I tried to match is with my pattern?

import java.util.regex.Pattern;
public class Main {
    public static void main(String[] args) {
        String pattern = "[\\p{Alpha} '-,.]";
        //String text = "(";
        String text = ")";
        System.out.print(Pattern.matches(pattern, text));
    }
}

Solution

  • This is because '-, is interpeted as range and is equivalent to '()*+,, try to escape minus character "[\\p{Alpha} '\\-,.]"