What is the use of Pattern.quote method?

后端 未结 6 1940
春和景丽
春和景丽 2020-11-27 02:59

I\'m trying to understand Pattern.quote using the following code:

String pattern = Pattern.quote("1252343% 8 567 hdfg gf^$545");
System         


        
6条回答
  •  青春惊慌失措
    2020-11-27 03:30

    The Pattern.quote method quotes part of a regex pattern to make regex interpret it as string literals.

    Say you have some user input in your search program, and you want to regex for it. But this input may have unsafe characters so you can use

    Pattern pattern = Pattern.compile(Pattern.quote(userInput));
    

    This method does not quote a Pattern but, as you point out, wraps a String in regex quotes.

提交回复
热议问题