in reply to Regex To remove text between parentheses

what I have so far is:
s/\((.*)\)//g;
but this gets rid of the whole string :-(

"Sanity is the playground of the unimaginative" -Unknown

Replies are listed 'Best First'.
Re: Re: Regex To remove text between parentheses
by lshatzer (Friar) on Jul 10, 2001 at 18:40 UTC

    You might want to look at Death to Dot Star! to why it is getting rid of the whole thing

    s/\([^)]+\)//g;

    (Unless you have nested parenteses, as Hofmator pointed out.) And if you have (), the above won't remove it, unless you change the + to a *.

    Also, any reason why you put the contents between the ()'s into $1? Are you going to use it later?

Re: Re: Regex To remove text between parentheses
by arturo (Vicar) on Jul 10, 2001 at 18:45 UTC

    Remember that * is *greedy* ... it will match the longest possible substring. So, given a string like

    (*) Microsoft Internet Exploder (which calls itself "Mozilla") - a pro +duct of a convicted violator of the Sherman Act (wow)

    That greedy quantifier will snatch up that * after the opening paren, and not stop till it gets to the ) that follows the "wow" (that IS the longest substring that matches your RE).

    As to the suggestion you use a character class (hint: match one or more things that aren't closing parentheses), see Death to Dot Star! for a discussion of why not to use .*

    HTH!

    perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'
Re: Re: Regex To remove text between parentheses
by davorg (Chancellor) on Jul 10, 2001 at 18:40 UTC

    You're being bitten by the "greediness" of regular expressions. They try to match as much of the string as possible - which, in this case, is all of it.

    To make the regex "non-greedy" put a '?' after the greedy part of the regex.

    You might also like to take a look at Death to Dot Star!

    --
    <http://www.dave.org.uk>

    Perl Training in the UK <http://www.iterative-software.com>

Re: Re: Regex To remove text between parentheses
by RatArsed (Monk) on Jul 10, 2001 at 18:36 UTC
    As a quick, cludgy fix; try (.+) instead of (.*)
    Update: OK, so this headache is affecting my mental powers -- ignore that previous bit and instead look at the answers invloving negated classes (^)*) and the like - In my defence, I said that was what you're supposed to do below:

    As a better fix, create a character class that doen't include brackets, and use that instead...

    --
    RatArsed, in search of enlightment and asprin.

A reply falls below the community's threshold of quality. You may see it by logging in.