How to escape text for regular expression in Java

前端 未结 8 1827
我在风中等你
我在风中等你 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:40

    It may be too late to respond, but you can also use Pattern.LITERAL, which would ignore all special characters while formatting:

    Pattern.compile(textToFormat, Pattern.LITERAL);
    

提交回复
热议问题