7

How to replace all "(" and ")" in a string with a fullstop, in Java? I tried in the following way:

String url = "https://bitbucket.org/neeraj_r/url-shortner)";
url.replaceAll(")", ".");
url.replaceAll(")", ".");

But it does not work. The error is:

Exception in thread "main" java.util.regex.PatternSyntaxException: Unmatched closing
')'
 )
at java.util.regex.Pattern.error(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.util.regex.Pattern.<init>(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.lang.String.replaceAll(Unknown Source)
at com.azzist.cvConversion.server.URLChecker.main(URLChecker.java:32)

I think this problem will be there in all regex too. Adding \ before ) did not work.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Neeraj
  • 1,612
  • 7
  • 29
  • 47

9 Answers9

18

You can use replaceAll in one go:

url.replaceAll("[()]", ".")

Explanation:

  • [()] matches both ( and ), the brackets don't need to be escaped inside the [] group.

EDIT (as pointed out by @Keppil):

Note also, the String url is not changed by the replace, it merely returns a new String with the replacements, so you'd have to do:

url = url.replaceAll("[()]", ".");
beny23
  • 34,390
  • 5
  • 82
  • 85
15

You need to escape '(' and ')' with "\\(" and "\\)" respectively:

url = url.replaceAll("\\)", ".");  
url = url.replaceAll("\\(", ".")
munyengm
  • 15,029
  • 4
  • 24
  • 34
  • There is a small mistake in your answer. You are only replacing ")". But also this will not work. Your logic will not work for "(". url = url.replaceAll("\\(", "."); will not replace "(" – Neeraj Sep 25 '12 at 06:45
  • It works using java 1.8+. Need to add 2 back slashes \\ in front of ( and ). url = url.replaceAll("\\\)", "."); is good – Supercoder Nov 11 '20 at 22:37
8

replaceAll() expects a regex as first parameter, and parantheses have special meaning there.

use replace() instead:

String url = "https://bitbucket.org/neeraj_r/url-shortner)";
url = url.replace("(", ".").replace(")", ".");

Note that the url = part is important to save the result of the replacement. Since Strings are immutable, the original String isn't changed, but a new one is created. To keep the result url needs to point to this one instead.

Keppil
  • 45,603
  • 8
  • 97
  • 119
  • Both 'url.replace("(",".") and url.replaceAll("\\(",".") work fine. The point here is "url = url.replace(...)". In the question the "url = ..." was missing. Thus the string will be replaced but not written somewhere. – Martin Preusse Sep 12 '12 at 08:25
  • 1
    @emempe: Yes, that was important too, but the error in OPs example was due to a regex error. I'll add a note about the `url =` part too. – Keppil Sep 12 '12 at 08:29
  • @Keppil I want to convert all the "(" in the String. By using .replace, I think I can convert only the first occurrence. – Neeraj Sep 13 '12 at 05:28
  • @Neeraj: No, `replace()` takes care of all of them. – Keppil Sep 13 '12 at 05:45
2
  1. String is immutable.
  2. ) should be escaped with 2 backslashes.

So the code would look like this:

String url = "https://bitbucket.org/neeraj_r/url-shortner)";
// is you need to escape all of them, use "[()]" pattern, instead of "\\)"
String s = url.replaceAll("\\)", "."); 
System.out.println(url);
System.out.println(s);

And the output:

https://bitbucket.org/neeraj_r/url-shortner)
https://bitbucket.org/neeraj_r/url-shortner.
Ostap Andrusiv
  • 4,827
  • 1
  • 35
  • 38
1

Adding a single \ will not work, because that will try to evaluate \) as an escaped special character which it isn't.

You'll need to use "\)". The first \ escapes the second, producing a "normal" \, which in turn escapes the ), producing a regex matching exactly a closing paranthesis ).

The general purpose solution is to use Pattern.quote, which takes an arbitrary string and returns a regex that matches exactly that string.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
1

You also could use org.apache.commons.lang.StringUtils.replace(String, String, String) which doesn't use regex

From apache commons library http://commons.apache.org/

maximede
  • 1,763
  • 14
  • 24
0

Method String#replaceAll expects a regular expression and ( as well as ) are special characters (marking the group) so you need to escape them to be non-special url.replaceAll("\\)", ".");.

Also you can replace both character at once with pattern url.replaceAll("[()]", ".");. You can see that in this context the brackets are not escaped. That is because of regEx context - inside of [] they don't have any special meaning.

Look at JavaDoc for more info.

Gaim
  • 6,734
  • 4
  • 38
  • 58
0

Because ) is character of regex, so you need to escape it. Try this:

url.replaceAll("\)", "\.");
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
0

Use

url.replaceAll("\\)", ".").replaceAll("\\(", ".");;
jmj
  • 237,923
  • 42
  • 401
  • 438