If you do not absolutely need to use regex, use consider using Perl Text :: Balanced to remove the brackets.
use Text::Balanced qw(extract_bracketed); my ($extracted, $remainder, $prefix) = extract_bracketed( $filename, '()', '[^(]*' ); { no warnings 'uninitialized'; $filename = (defined $prefix or defined $remainder) ? $prefix . $remainder : $extracted; }
You might be thinking, "Why all this when a regexp does the trick on one line?"
$filename =~ s/\([^}]*\)//;
Text :: Balanced processes nested parentheses. Thus, $filename = 'foo_(bar(baz)buz)).foo' will be extracted correctly. The regex solutions offered here will fail on this line. One will stop at the first close, and the other will eat them all.
$ filename = ~ s / ([^}] *) //; # returns' foo_buz)). foo '
$ filename = ~ s /(.*)//; # returns 'foo_.foo'
# text balanced example returns' foo _). foo '
If any of the regular expression behaviors is acceptable, use a regular expression - but document the limitations and assumptions made.
daotoad Mar 12 '09 at 22:55 2009-03-12 22:55
source share