regex java replace all

0
String url = "/users/{id}/books/{id_book}";

url = url.replaceAll("{\w*}", "\w*");

System.out.println("result url:" + url);

Trying to do a replace with regex, and returns me the following error:

Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition
{\w*}
    at java.util.regex.Pattern.error(Pattern.java:1955)
    at java.util.regex.Pattern.closure(Pattern.java:3157)
    at java.util.regex.Pattern.sequence(Pattern.java:2134)
    at java.util.regex.Pattern.expr(Pattern.java:1996)
    at java.util.regex.Pattern.compile(Pattern.java:1696)
    at java.util.regex.Pattern.<init>(Pattern.java:1351)
    at java.util.regex.Pattern.compile(Pattern.java:1028)
    at java.lang.String.replaceAll(String.java:2223)
    at br.com.rileyframework.TestRegex.main(TestRegex.java:10)

Result I hope:

/users/\w*/books/\w*
    
asked by anonymous 11.03.2017 / 13:22

1 answer

2

You need to escape from the keys.

String url = "/users/{id}/books/{id_book}";
url = url.replaceAll("\{\w*\}", "\\w*");
System.out.println("result url:" + url);
    
11.03.2017 / 14:20