Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Text elimination within a string

by cdherold (Monk)
on Apr 08, 2001 at 06:55 UTC ( [id://70772]=perlquestion: print w/replies, xml ) Need Help??

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

Brother monks, I have a string, $headline, that contains the following:

( BW)(CA-LIGAND-PHARMACEUTICALS)(LGND) Ligand's Targretin Capsules Approved in Europe, Targretin Gel European Application Submitted; Ligand to Receive $3.5 Million Milestone Payments From Elan

Is there any quick expression I can use to eliminate all the text between the first and last parentheses -- "(" and ")" -- as well as the flanking parentheses themselves?

cdherold

Replies are listed 'Best First'.
Re: Text elimination within a string
by cLive ;-) (Prior) on Apr 08, 2001 at 07:13 UTC
    s/^\(.*\)//;

    As long as you are sure there will be no braces in the following text.

    If text in braces is always in upper case, you could use:

    s/^\([^a-z]+\)//;

    to eliminate that possible problem.

    cLive ;-)

    Update: - added some hats to the regex (forgot them earlier...)

      Though, if there is a chance that there would be parens in the later part of the string...

      (BW)(CA-LIGAND-PHARMACEUTICALS)(LGND) Ligand's Targretin Capsules Approved in Europe, Targretin Gel European Application Submitted; Ligand to Receive $3.5 Million (¥433.3 Million) Milestone Payments From Elan

      ... that regex would take out most of the wanted text.

      s/^(\(.*?\))*//;
      That will only take of the leading text encased in parens.

      - FrankG

        Add a g as option to your s/// and might be right. Still you should consider using the following regex that doesn't use .*:
        s/\([^)]\)//g;
        Which will strip off all braced strings, including the braces.
        --
        Alfie
        um, this regex doesn't work - I think you meant .* at end, but even then it won't work because it would only match the first set of braces.

        cLive ;-)

        Update: - ah yes, with a g. I also "saw" a $1 that wasn't there. Otherwise, why (remember) it?

      Ofcourse you could consider the Death to Dot Star! node :|

      Greetz
      Beatnik
      ... Quidquid perl dictum sit, altum viditur.
        Except in this case, a greedy match is required.

        The second example is 'death to dot star' compliant, but is still making an assumption (that all text in braces to be removed is in upper case)

        Using:

        \([^)]*\)
        would only match first set of braces.

        So .* is appropriate here. honest :)

        cLive ;-)

        Update: Should also begin match with a ^. Hmmm, oops.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://70772]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-23 22:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found