java Dangling meta character ‘+’ [closed]

You error has nothing to do with the shown regex.

The problem is because you use the matched result values as a parameter to replaceAll(), and those parameters are also regular expressions.

Since you don’t want them to be interpreted as regex, you need to escape them, or rather “quote” them, like this:

str = str.replaceAll(Pattern.quote(matcher.group()),
                     Matcher.quoteReplacement('"' + matcher.group() + '"'));

UPDATE

However, if you just want to put double-quotes around the matched strings, why don’t you just use replaceAll() directly? Like this:

    str = str.replaceAll("(?<==)([+a-zA-Z0-9]+)(?=[> ])", "\"$1\"");

Leave a Comment