How to ensure replaceAll will replace a whole word and not a subString

后端 未结 2 1383
执笔经年
执笔经年 2020-12-19 08:16

I have an input of dictionary. The dictionary is iterated over to replace the key from dictionary in the text. But replaceAll function replaces the

2条回答
  •  不思量自难忘°
    2020-12-19 09:18

    replaceAll takes as parameter a regular expression.

    In regular expressions, you have word boundaries : \b (use \\b in a string literal). They're the best way to ensure you're matching a word and not a part of a word : "\\bword\\b"

    But in your case, you can't use word boundaries as you're not looking for a word ([69-3] isn't a word).

    I suggest this :

    text=text.replaceAll("(?=\\W+|^)"+Pattern.quote("[69-3]")+"(?=\\W+|$)", ...
    

    The idea is to match a string end or something that's not a word. I can't ensure this will be the right solution for you though : such a pattern must be tuned knowing the exact complete use case.

    Note that if all your keys follow a similar pattern there might be a better solution than to iterate through a dictionary, you might for example use a pattern like "(?=\\W+|^)\\[\\d+\\-\\d+\\](?=\\W+|$)".

提交回复
热议问题