OK, I got a weird one that I've been jamming on for awhile (fri afternoon mind does not work I guess).
Does anyone know of a away to parse a string and remove all of the text inside parens without removing the parens themselves...but with deleting parens found inside.
ie.
myString = "this is my string (though (I) need (help) fixing it)"
after running it through what I want it would look like:
myString = "this is my string ()"
very important to keep those two parens there.
The module Regexp::Common deals with more than 1 top level of parentheses.
use strict;
use warnings;
use Regexp::Common qw/balanced/;
my @strings = (
'111(22(33)44)55',
'a(b(c(d)(e))f)g(h)((i)j)',
'this is my string (though (I) need (help) fixing it)',
);
s/$RE{balanced}{-parens=>'()'}/()/g for @strings;
print "$_\n" for @strings;
Output:
111()55 a()g()() this is my string ()
You need to escape the parentheses to prevent them from starting a capture group. The pattern \(.+\)
match the longest substring that starts with a (
and ends with a )
. That will gobble up everything up to the last )
including any intervening parentheses. Finally, we replace that string with one containing just ()
:
#!/usr/bin/perl
use strict; use warnings;
my $s = "this is my string (though (I) need (help) fixing it)";
$s =~ s{\(.+\)}{()};
print "$s\n";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With