Remove data from within a String

1

In java I'm reading several lines from an HTML file and in it I have to pull some data and store in variables. And to remove this data I have to remove some tags and data that are within certain patterns.

For example, to remove the tags I use replaceAll("\<.*?>", ""); , but now I need to remove from String everything inside parentheses. I tried to use the code replaceAll("\(.*?)", ""); , but it did not work very well.

    
asked by anonymous 09.09.2017 / 14:32

2 answers

1

You have to "escape" the parenthesis, otherwise it is considered a group:

"Texto qualquer (texto)".replaceAll("\(.*\)", "")
    
09.09.2017 / 15:50
0

The code below has been adapted from this answer and finds the texts in parentheses (including parentheses:

String str = "Teste (Java)";
String parenteses = str.substring(str.indexOf("("), str.indexOf(")") + 1);

If you need to remove all string text (including parentheses), you can create a loop :

String str = "Teste (Java)";

while (str.indexOf("(") >= 0 && str.indexOf(")") >= 0) {
    String parenteses = str.substring(str.indexOf("("), str.indexOf(")") + 1);
    str = str.replaceAll(parenteses);
}
    
09.09.2017 / 14:51