How to escape text for regular expression in Java

前端 未结 8 1932
我在风中等你
我在风中等你 2020-11-22 03:30

Does Java have a built-in way to escape arbitrary text so that it can be included in a regular expression? For example, if my users enter \"$5\", I\'d like to match that exa

8条回答
  •  独厮守ぢ
    2020-11-22 03:54

    Difference between Pattern.quote and Matcher.quoteReplacement was not clear to me before I saw following example

    s.replaceFirst(Pattern.quote("text to replace"), 
                   Matcher.quoteReplacement("replacement text"));
    

提交回复
热议问题