2

How can i do this in java :

given a string, for each text in parenthesis, if it contains "blabla" then remove all text in these parenthesis including parenthesis themselves

example :

some string (some text) (blabla foo bar) => some string (some text)
some string (some text) (blabla) => some string (some text)
some string (ex ex) => unchanged (because words in parenthesis doesn't contain "blabla")

thanks.

3 Answers 3

3

You could try the below replaceAll function.

string.replaceAll("\\([^()]*blabla[^()]*\\)", "");

Explanation:

  • \\( Matches a literal ( symbol.
  • [^()]* Negated character class which matches any character but not of ( or ) zero or more times.
  • blabla Matches the string blabla
  • [^()]* Negated character class which matches any character but not of ( or ) zero or more times.
  • \\) Matches a literal ) symbol.
Sign up to request clarification or add additional context in comments.

Comments

1
\\(.*?\\bblabla\\b.*?\\)

Try this.Replace by empty string.

http://regex101.com/r/zU7dA5/12

Use \b to make sure you replace only blabla and not abcblablaas

Comments

0
 String x = "some string (some text) (blabla)";
    String replace = x.replace("(blabla)", "");
    System.out.println(""+replace);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.