Do I always need to escape metacharacters in a string that is not a “literal”?

后端 未结 4 1528
耶瑟儿~
耶瑟儿~ 2021-01-12 13:53

It seems that having a string that contains the characters { or } is rejected during regex processing. I can understand that these are reserved cha

4条回答
  •  孤街浪徒
    2021-01-12 14:09

    Use Pattern.quote(String):

    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.

    Parameters:
        s - The string to be literalized
    Returns:
        A literal string replacement
    Since:
        1.5

提交回复
热议问题