-3

I have a string like "18+, a + b". Can I replace all symbol "+" by "\+" in Java 11.

I try replaceAll("+", "\+") but failed by Dangling meta character '+' Exception in

thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0
+
^
    at java.base/java.util.regex.Pattern.error(Pattern.java:2045)
    at java.base/java.util.regex.Pattern.sequence(Pattern.java:2220)
    at java.base/java.util.regex.Pattern.expr(Pattern.java:2086)
    at java.base/java.util.regex.Pattern.compile(Pattern.java:1798)
    at java.base/java.util.regex.Pattern.<init>(Pattern.java:1445)
    at java.base/java.util.regex.Pattern.compile(Pattern.java:1084)
    at java.base/java.lang.String.replaceAll(String.java:2950)

Please help me, thank you

  • 3
    I don't see why regex is needed at all. You want to replace one char with another, use the non regex `replace(char, char)` method. (It also replaces all occurances, just doesn't use regular expressions). – OH GOD SPIDERS Jul 10 '23 at 09:05
  • 2
    You probably meant - `replaceAll("\\+", "+");`? The first parameter is the regex, the second is the replacement. But why do you want to replace a character with the **same** character? The whole operation is redundant. – Chaosfire Jul 10 '23 at 09:06
  • Update: It working with '"18+ , a + b".replaceAll("\\+", "\\\\+")' – NguyenLT21 Jul 10 '23 at 09:23

2 Answers2

1

TL;DR: You want .replace. Not .replaceAll. Which will replace all occurrences (Yes, .replace replaces all, as does replaceAll) - and won't complain about dangling metacharacters.


Here's how it breaks down:

  • a.replace("needle", "duck") turns all needles into ducks.
  • a.replaceAll("needleRegex", "duck") parses the first string argument as a regular expression (which is a programming language-esque concept to describe regular grammars, and + has significance in this language, hence why you got your error). Then replaces all substrings within a that match the regex with "duck". .replaceAll is the regular expression version, .replace is the plain strings version.
  • a.replaceFirst("needleRegex", "duck") is like replaceAll, except it stops after replacing the first substring it finds.

In other words, All/First contrast to each other, and these methods are very badly named / should never have been part of java1. The appropriate methods, if they are to exist at all, either [A] should have taken a Pattern as first argument (java is strongly nominally typed, if there's a type that represents the thing, use it), or [B] should have been named .regexReplaceAll instead.

As a consequence I strongly advise all to never use .replaceAll. If you really want to futz with regular expressions (can be quite powerful, but you should want them, you don't want some code to arbitrarily apply regex parsing as a surprise), make a pattern, use its replace-all methods instead. That way, your code is more flexible (Example: If parsing the regex is part of a profiler's performance tight loop, you can isolate it. You can explicitly set parameters, and use the vast array of methods pattern/matcher expose, instead of just replaceAll/replaceFirst/matches) - and nobody will be confused when reading it: You are using regexes, it is obvious that you are, and it is obvious that you intended to.


1 Java tends to take backwards compatibility fairly seriously; as a consequence I doubt these methods will be removed or even marked as obsolete/deprecated/ill-advised, because that would break existing code. However, the ecosystem may (should!) make a move; linter tools should ship out of the box advising against it, IDEs should remove or at least visually disencourage these methods in their auto-complete dialogs, and so on.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
1

"... Can I replace all symbol "+" by "+" in Java 11. ..."

Yes, you can use the String#replace method.
The String#replaceAll method isn't required here, it is an alternative to replace which allows for a regular expression pattern as input.

String string = "18+, a + b";
string = string.replace("+", "\\+");

Output

18\+, a \+ b
Reilas
  • 3,297
  • 2
  • 4
  • 17