Replace all "(" and ")" in a string in Java

How to replace all "(" and ")" in a string using fullstop in Java? I tried as follows:

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

But that will not work. Error:

 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 in all regular expressions. Adding \ before ) does not work.

+6
source share
9 answers

You can use replaceAll at a time:

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

Explanation:

  • [()] matches both ( and ) , brackets should not be escaped within the group [] .

EDIT (as @Keppil points out):

Note that the url string is not replaced by a replacement, it just returns a new replacement string, so you will need:

 url = url.replaceAll("[()]", "."); 
+14
source

You need to output '(' and ')' using "\\(" and "\\)" respectively:

 url = url.replaceAll("\\)", "."); url = url.replaceAll("\\(", ".") 
+11
source

replaceAll() expects regex as the first parameter, and there is a special meaning in brackets.

use replace() instead:

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

Note that the url = part is important for saving the replacement result. Because String immutable, the original String not changed, but a new one is created. To save the url result, you need to indicate this.

+7
source
  • The string is unchanged.
  • ) should be escaped using two backslashes.

Thus, the code will 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 conclusion:

 https://bitbucket.org/neeraj_r/url-shortner) https://bitbucket.org/neeraj_r/url-shortner. 
+2
source

Adding one \ will not work because it will try to evaluate \) as an escaped special character, which it is not.

You will need to use "\" ". The first \ escapes the second, creating a" normal " \ , which in turn exits ) , creating a regular expression that matches the exact closing parenthesis ) .

A general-purpose solution is to use Pattern.quote , which takes an arbitrary string and returns a regular expression that matches that particular string.

+1
source

You can also use org.apache.commons.lang.StringUtils.replace (String, String, String) which does not use regex

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

+1
source

The String#replaceAll expects the regular expression and ( and also ) be special characters (group designation), so you need to avoid their non- url.replaceAll("\\)", "."); .

Also, you can immediately replace both characters with the pattern url.replaceAll("[()]", "."); . You can see that in this context, the brackets are not escaped. This is due to the regEx context - inside [] they have no special meaning.

Check out the JavaDoc for more information.

0
source

Because ) is a regular expression symbol, so you need to avoid it. Try the following:

 url.replaceAll("\)", "\."); 
0
source

Using

 url.replaceAll("\\)", ".").replaceAll("\\(", ".");; 
0
source

Source: https://habr.com/ru/post/925202/


All Articles