in reply to How to use regex to get brackets surrounding something

The original question: I have a data looking like : $data = (D,(A,B),C) When searching for A, I want to extract (A,B) and cut it out form the $data. I am not sure how to get the nearest "(" of A. Is there a regex for that?

Not the clearest description, but if you mean you want to search for 'A', and remove it and everything within the nearest set of enclosing parens:

$data = '(D,(A,B),C)';; $data =~ s{(\([^(]*A[^)]*\))}{} and print "$1\n$data";; (A,B) (D,,C)

Note: the stuff removed is also available in $1.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: How to use regex to get brackets surrounding something
by lazycoder (Initiate) on Apr 09, 2016 at 02:03 UTC
    Yup thats what I wanted. Thanks!
Re^2: How to use regex to get brackets surrounding something
by lazycoder (Initiate) on Apr 09, 2016 at 02:14 UTC
    I missed something. I updated the question.