Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing text inside parens, but not the parens in Perl

Tags:

string

perl

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.

like image 598
James Avatar asked Feb 12 '10 22:02

James


2 Answers

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 ()
like image 146
Chris Charley Avatar answered Sep 28 '22 08:09

Chris Charley


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";
like image 41
Sinan Ünür Avatar answered Sep 28 '22 06:09

Sinan Ünür