Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to strip text from brackets which appears anywhre in a string $desc. This should also remove the brackets. I was trying
$desc =~ s/\(.*?\)//gs;
But this does not work where the brackets contain only text. How can I strip out brackets containing any sort of text from a string? Or just empty brackets.

Replies are listed 'Best First'.
Re: Strip brackets and contents from string
by hippo (Archbishop) on Apr 11, 2016 at 14:45 UTC
    But this does not work where the brackets contain only text.

    Works for me:

    #!/usr/bin/env perl use strict; use warnings; use Test::More; my %foo = ( 'a(b)c' => 'ac', 'ab()cd' => 'abcd' ); my $snr = qr/\(.*?\)/; for my $have (keys %foo) { my $want = $foo{$have}; $have =~ s/$snr//sg; is ($have, $want); } done_testing ();

    "It doesn't work" is as useful to a developer as "I'm ill" is to a medic. Try to be more specific.

      "It doesn't work" is as useful to a developer as "I'm ill" is to a medic. Try to be more specific.

      ++ for the accurate analogy. If I could cast another vote for the content of your post I would :)


      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.
Re: Strip brackets and contents from string
by AnomalousMonk (Archbishop) on Apr 11, 2016 at 14:58 UTC

    Maybe you're referring to nested parenthetics, which your regex indeed doesn't handle properly.

    c:\@Work\Perl\monks>perl -wMstrict -le "my $desc = 'leave (only text) this () too -- does not (v(w(x)y)z) wor +k'; $desc =~ s/\(.*?\)//gs; print qq{'$desc'}; " 'leave this too -- does not y)z) work'
    Can you elucidate?

    Update: If you're concerned about nested parens, maybe see Regexp::Common::balanced (used as below):

    c:\@Work\Perl\monks>perl -wMstrict -le "use Regexp::Common qw(balanced); ;; my $desc = 'a (text) b () c (v(w(x)y)z) d ((())) e (unbalanced(balanc +ed) f'; $desc =~ s{ $RE{balanced}{-parens => '()'} }{}xmsg; print qq{'$desc'}; " 'a b c d e (unbalanced f'
    (Update update: Regexp::Common::balanced is 5.8 compatible.)


    Give a man a fish:  <%-{-{-{-<

Re: Strip brackets and contents from string
by jellisii2 (Hermit) on Apr 11, 2016 at 15:06 UTC
    This whiffs of an XY Problem. Can you provide sample data and a more precise articulation of the issue?
Re: Strip brackets and contents from string
by james28909 (Deacon) on Apr 11, 2016 at 14:57 UTC
    use strict; use warnings; my $string = 'this is (testing 123(test%^*&()() fghfgh(in)))*&^@#%$))))(( ((( (g) 123)a test'; $string =~ s/\(.*\)//gs; print $string;
    Updated: Swapped to /s modifier to allow for multiple lines.
    UPDATE 2: Added /g modifier back into regex.
Re: Strip brackets and contents from string
by SimonPratt (Friar) on Apr 11, 2016 at 15:27 UTC

    Like this? $desc =~ s/(\(([^()]|(?R))*\))//g;

Re: Strip brackets and contents from string
by BillKSmith (Monsignor) on Apr 11, 2016 at 20:48 UTC
    Perhaps your string contains newlines. The metacharacter '.' does not match newline. Use s/\{[^}]*\}//gms. (This is probably good practice even if it is not your problem.
    Bill