How to escape text for regular expression in Java

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

    I think what you're after is \Q$5\E. Also see Pattern.quote(s) introduced in Java5.

    See Pattern javadoc for details.

提交回复
热议问题