Replace all "(" and ")" in a string in Java
Asked Answered
A

9

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.

Anklet answered 12/9, 2012 at 8:10 Comment(1)
add \\ not \ . you need to add two double slashesCommercialism
C
19

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("[()]", ".");
Code answered 12/9, 2012 at 8:13 Comment(0)
M
15

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

url = url.replaceAll("\\)", ".");  
url = url.replaceAll("\\(", ".")
Manaus answered 12/9, 2012 at 8:12 Comment(2)
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 "("Anklet
It works using java 1.8+. Need to add 2 back slashes \\ in front of ( and ). url = url.replaceAll("\\)", "."); is goodPainty
H
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.

Hyperphysical answered 12/9, 2012 at 8:12 Comment(4)
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.Gearldinegearshift
@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.Hyperphysical
@Hyperphysical I want to convert all the "(" in the String. By using .replace, I think I can convert only the first occurrence.Anklet
@Neeraj: No, replace() takes care of all of them.Hyperphysical
B
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.
Bergen answered 12/9, 2012 at 8:17 Comment(0)
P
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.

Plaything answered 12/9, 2012 at 8:13 Comment(0)
P
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/

Profluent answered 12/9, 2012 at 10:23 Comment(0)
B
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.

Bryant answered 12/9, 2012 at 8:13 Comment(0)
C
0

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

url.replaceAll("\)", "\.");
Cementite answered 12/9, 2012 at 8:13 Comment(0)
T
0

Use

url.replaceAll("\\)", ".").replaceAll("\\(", ".");;
Tarr answered 12/9, 2012 at 8:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.