How to escape a square bracket for Pattern compilation

后端 未结 4 1478
萌比男神i
萌比男神i 2020-12-03 10:16

I have comma separated list of regular expressions:

.{8},[0-9],[^0-9A-Za-z ],[A-Z],[a-z]

I have done a split on the comma. Now I\'m trying

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-03 10:33

    You can use Pattern.quote(String).

    From the docs:

    public static String quote​(String s)

    Returns a literal pattern String for the specified String.

    This method produces a String that can be used to create a Pattern that would match the string s as if it were a literal pattern.

    Metacharacters or escape sequences in the input sequence will be given no special meaning.

提交回复
热议问题