-3

I looked at a stack overflow which gave me this regex- str.replaceAll("\\(^()*\\)", ""); but when I ran it, it didn't actually do anything. so how do I remove from a given string any section which is within a Parentheses including the Parentheses? also: Other than parentheses only letters and spaces can occur in the string. Don't worry about other brackets like "[]" and "{}" as these are not included, I don't want to touch "[]" and "{}" if they do appear. I only want to remove the Parentheses and the contents inside the Parentheses. help would be appreciated. this also includes multiple parentheses and nested parentheses.

    public static String removeParentheses(final String str) {
        str.replaceAll("\\(^()*\\)", "");
        System.out.println(str);
            return str; // your code here
        }
       public static void main(String[] args) {
            String str= "example(unwanted thing)example";
            removeParentheses(str);
}
    ```
expected result is "exampleexample" but actual result was: "example(unwanted thing)example" which is not what I wanted.
I already searched stack overflow for help and found one place but it didn't help me.
help would be appreciated.
Gil Caplan
  • 62
  • 8
  • ```public class qqqq { public static String removeParentheses(final String str) { str.replaceAll("\\(^(.*)*\\)", ""); System.out.println(str); return str; // your code here } public static void main(String[] args) { String str= "example(unwanted thing)example"; removeParentheses(str); } } ``` – Gil Caplan Nov 15 '20 at 06:02
  • result was: example(unwanted thing)example – Gil Caplan Nov 15 '20 at 06:02
  • 1
    Regex `"\\(^()*\\)"` means to match an open-parenthesis (`(`) followed by the beginning of input (`^`). Since the beginning of input obviously cannot be following anything, the regex cannot ever match anything. --- It appears that you were trying to use a negated character class `[^X]`, so the regex should be `"\\([^()]*\\)"`. meaning an open-parenthesis, followed by zero-or-more non-parenthesis characters, followed by a close-parenthesis. – Andreas Nov 15 '20 at 06:02
  • Comments section is for clarifications, not for posting Answers – Patrick Parker Nov 15 '20 at 06:07
  • @PatrickParker I commented because I consider the problem to be a *typo*, and not something that others will find useful. I am of the belief that the question should be deleted. – Andreas Nov 15 '20 at 06:09
  • Does this answer your question? [Is it possible to match nested brackets with a regex without using recursion or balancing groups?](https://stackoverflow.com/questions/47162098/is-it-possible-to-match-nested-brackets-with-a-regex-without-using-recursion-or) – Patrick Parker Nov 15 '20 at 06:24
  • not really, ```return str.replaceAll("(?=\()(?:(?=.*?\((?!.*?\1)(.*\)(?!.*\2).*))(?=.*?\)(?!.*?\2)(.*)).)+?.*?(?=\1)[^(]*(?=\2$");``` got error when i tried to use it plus its kinda long. thanks for the help – Gil Caplan Nov 15 '20 at 06:36
  • Gil -- that's because you failed to escape the backslashes when you embedded the regex in a Java String literal – Patrick Parker Nov 15 '20 at 06:45
  • i managed when i tryed again but when I ran it I got this error-bound must be positive – Gil Caplan Nov 15 '20 at 06:54
  • i guess that means its checking when its empty. so i would use an if to get rid of the option of when its empty but ```string.length()``` or ```string.size()``` doesn't seem to work – Gil Caplan Nov 15 '20 at 06:59
  • same thing with ```str.equals("")``` – Gil Caplan Nov 15 '20 at 07:01

1 Answers1

1

corrected 2 issues

  1. printing incorrect variable
  2. regex
    public static void main(String[] args) {
        removeParentheses2("example(unwanted thing)example");
        removeParentheses2("abc(123)def(456)ghi");
        removeParentheses2("abc (123) def (456) ghi");
        removeParentheses2("abc(1|3)def(4^6)gh");
        removeParentheses2("abc(((1|3)def(4^6)gh))");

    }

    public static String removeParentheses(final String str) {
        String updated = str.replaceAll("\\([^()]*\\)", "");
        if (updated.contains("(")) updated = removeParentheses(updated);
        System.out.println(str + " >> " + updated);
        return updated;
    }


    // a more direct regex - no loop
    static final String regex = "\\([^()]*\\)";
    static final String regex_match = "\\([" + regex + "]*\\)";
    public static String removeParentheses2(final String str) {
        String updated = str.replaceAll(regex_match, "");
        System.out.println(str + " >> " + updated);
        return updated;
    }



example(unwanted thing)example >> exampleexample
abc(123)def(456)ghi >> abcdefghi
abc (123) def (456) ghi >> abc  def  ghi
abc(1|3)def(4^6)gh >> abcdefgh
abc(defgh) >> abc
abc((1|3)def(4^6)gh) >> abc
PrasadU
  • 1,147
  • 1
  • 7
  • 9
  • Fail! Example: `"abc(123)def(456)ghi"` should return `"abcdefghi"`, but it doesn't. – Andreas Nov 15 '20 at 06:11
  • updated the sol – PrasadU Nov 15 '20 at 06:16
  • Fail! Example: "abc(1|3)def(4^6)ghi" should return "abcdefghi", but it doesn't. --- Why are `|` and `^` characters not allowed between parentheses? *FYI:* Regex character class `[^\(|^\)]` means "any character other than `(`, `|`, `^`, and `)`". – Andreas Nov 15 '20 at 06:22
  • *FYI:* There is no need to escape parentheses inside a character class, since they have no special meaning there. – Andreas Nov 15 '20 at 06:23
  • also. ```"(first group) (second group) (third group)"``` expected is two white spaces but I get zero spaces. how can I ignore the spaces part of the regex? – Gil Caplan Nov 15 '20 at 06:26
  • Also, keep in mind the OP asked to match Nested parenthesis! which is very difficult to do in Java regex but has been answered before. – Patrick Parker Nov 15 '20 at 06:26
  • ok. thanks. "a(b(c))" -expected "a" but was a(b). anywhere you would suggest working on regex because all the demo's like regex 101 don't have java – Gil Caplan Nov 15 '20 at 06:29
  • @Gill - updated with new test - hope it helps you - note I am just trying to help. I hope you can play with the regs a bit now – PrasadU Nov 15 '20 at 06:37
  • recursive a(b(c)) - just do recursive call - updated the sol – PrasadU Nov 15 '20 at 06:52
  • "(infinite loops" and "(((assumptions about nesting depth)))" – Patrick Parker Nov 16 '20 at 04:33