Matching a whole word with leading or trailing special symbols like dollar in a string

后端 未结 4 1406
面向向阳花
面向向阳花 2020-12-19 05:36

I can replace dollar signs by using Matcher.quoteReplacement. I can replace words by adding boundary characters:

from = \"\\\\b\" + from + \"\         


        
4条回答
  •  长情又很酷
    2020-12-19 06:01

    $ has special meaning in regex (it means “end of input”). To remove any special meaning from characters in your target, wrap it in regex quote/unquote expressions \Q...\E. Also, because $ is not ”word” character, the word boundary won’t wiork, so use look arounds instead:

    line = line.replaceAll("(?

提交回复
热议问题