2

I have a list of strings that contains tokens.
Token is:

{ARG:token_name}.

I also have hash map of tokens, where key is the token and value is the value I want to substitute the token with.

When I use "replaceAll" method I get error:

java.util.regex.PatternSyntaxException: Illegal repetition

My code is something like this:

myStr.replaceAll(valueFromHashMap , "X"); 

and valueFromHashMap contains { and }.

I get this hashmap as a parameter.

omrid
  • 497
  • 3
  • 10
  • 20

4 Answers4

7

String.replaceAll() works on regexps. {n,m} is usually repetition in regexps.

Try to use \\{ and \\} if you want to match literal brackets.

So replacing all opening brackets by X works that way:

myString.replaceAll("\\{", "X");

See here to read about regular expressions (regexps) and why { and } are special characters that have to be escaped when using regexps.

Johannes Weiss
  • 52,533
  • 16
  • 102
  • 136
  • so I would have to iterrate my hashmap keys, detect if key contains { or } and add a slash before it? – omrid Feb 14 '12 at 15:54
  • No, the `String.replaceAll()` method uses regular expressions. These regular expressions have some kind of special characters (e.g. { and }). As a sample `a{5,7}` means "5 to 7 'a's in a row" (aaaaa, aaaaaa or aaaaaaa). I expected you tried something like `myStr.replaceAll("{", "X")` but `{` is not a valid regular expression. Therefore my hint: Just replace `{` by `\\{` and `}` by `\\}` in the first parameter of `String.replaceAll()`. – Johannes Weiss Feb 14 '12 at 16:00
  • Johannes Weiß, you are right I use myStr.replaceAll(valuefromhashmap, "X") and valueFromHashMap is "{". I recieve this hashmap as parameter. What should I do? replace all items in hashmap? – omrid Feb 14 '12 at 16:03
  • Ahh, now I get, use `myStr.replaceAll(Pattern.quote(valueFromHashMap), ...)`. As (at least) @Colin Hebert already said: `Pattern.quote()` quotes all special characters (e.g. `{`, `}`, `.`, ...) – Johannes Weiss Feb 14 '12 at 16:05
5

As others already said, { is a special character used in the pattern (} too). You have to escape it to avoid any confusion.

Escaping those manually can be dangerous (you might omit one and make your pattern go completely wrong) and tedious (if you have a lot of special characters). The best way to deal with this is to use Pattern.quote()


Related issues:

Resources:

Community
  • 1
  • 1
Colin Hebert
  • 91,525
  • 15
  • 160
  • 151
1

replaceAll() takes a regular expression as a parameter, and { is a special character in regular expressions. In order for the regex to treat it as a regular character, it must be escaped by a \, which must be escaped again by another \ in order for Java to accept it. So you must use \\{.

Aasmund Eldhuset
  • 37,289
  • 4
  • 68
  • 81
0

You can remove the curly brackets with .replaceAll() in a line with square brackets

String newString = originalString.replaceAll("[{}]", "X")

eg: newString = "ARG:token_name"

if you want to further separate newString to key and value, you can use .split()

String[] arrayString = newString.split(":")

With arrayString, you can use it for your HashMap with .put(), arrayString[0] and arrayString[1]

Lee Yi Hong
  • 1,310
  • 13
  • 17