Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove all the "() and text within it" in Java String

Tags:

java

string

regex

I need to ask about the deletion of parenthesis and text within them in Java String. For example, I have

String str = "I am a new (Software) Engineer"

Now the question is, how to remove the substring "(software)" here without using

str.replace("(software)", "")

because may be in next string I would get "I am a new (Electrical) Engineer" or "(Mechanical") or something like that.

So how can I do this, I think one way is to get the index of "(" and ")" and removing/replacing them by using that indices but I hope there will be some shorter way to do this

like image 440
Husnain Iqbal Avatar asked Oct 02 '13 11:10

Husnain Iqbal


People also ask

How do I remove a specific part of a string in Java?

Java allows you to easily replace a substring you want to delete from the String with an empty String. Syntax: string. replace(char oldChar, empty char)

How do I remove all characters from a string in Java?

You can use a regular expression and replaceAll() method of java. lang. String class to remove all special characters from String.


2 Answers

Use regex to match anything in brackets:

str = str.replaceAll("\\(.*?\\) ?", "");

Note that the brackets must be escaped, and that the escape character \ itself must be escaped in the String, hence the double backslashes.

The term .*? is a reluctant match, which means that it won't skip all the way to the last bracket in case there are two bracketed terms in the input ie "a (foo) b (bar) c" would become "a c" if .* was used, because it would consume everything from the first open bracket to the last close bracket.

I added an optional space at the end so you won't be left with two adjacent spaces once you remove the intervening term.

like image 192
Bohemian Avatar answered Oct 06 '22 14:10

Bohemian


Actually, the most efficient and foolproof way would be to go through the entire String and check for brackets. It would require only a single sweep.

You would also have to keep track of the amount of parenthesis opened and closed, to make sure the algorithm is correct for sentences like outside (a text (in another) parenthesis) outside again which should result in outside outside again.

A code which does more or less that.

public class RemoveParenthesis {
    public static void main(String[] args) {
        int open = 0;
        int closed = 0;
        boolean changed = true;
        int startIndex = 0, openIndex = -1, closeIndex = -1;
        String text = "outside (a text (in another) parenthesis) outside again";
        System.out.println("before: " + text);

        while (changed) {
            changed = false;
            for (int a = startIndex; a < text.length(); a++) {
                if (text.charAt(a) == '(') {
                    open++;
                    if (open == 1) {
                        openIndex = a;
                    }
                } else if (text.charAt(a) == ')') {
                    closed++;
                    if (open == closed) {
                        closeIndex = a;
                        text = text.substring(0, openIndex)
                                + text.substring(closeIndex + 1);
                        changed = true;
                        break;
                    }
                } else {
                    if (open == 0)
                        startIndex++;
                    continue;
                }
            }
        }

        System.out.println("after: " + text);
    }
}
like image 26
Dariusz Avatar answered Oct 06 '22 14:10

Dariusz