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.
|
|---|
| 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 | |
|
Re^2: How to use regex to get brackets surrounding something
by lazycoder (Initiate) on Apr 09, 2016 at 02:14 UTC |