Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Regxp: signaling when to stop

by Trizor (Pilgrim)
on Jun 20, 2007 at 15:14 UTC ( [id://622278]=note: print w/replies, xml ) Need Help??


in reply to Regxp: signaling when to stop

Simply use capturing groups like this: (You also want a non greedy operator as other posts have pointed out.)

#The regex, commented for your convinence. $Error =~ /Sequence\s #opening anchor, don't capture ( # open capturing group to get target pattern .+? # target pattern, non-greedy catch all ) # close group \sconsists # closing anchor, don't capture /x; # end regex

After that your target name will be inside $1. If you want to place it all on one line, you can use =~ in list context and get the results as a list like this:

($Missing_Living_Being) = $Error =~ /Sequence\s(.+?)consists/;

Just be sure that the list that you're assigning to has the same number of elements as there are capturing groups (the parts of the pattern inside parens) or else you'll run into trouble with values going to the wrong place. If you're paranoid about this you could assign to an array and shift it to get the first group, but that adds a lot of uneccessary overhead.

Edit: Forgot a semi-colon.
Edit 2: Made the assumption that all living beings have two word names.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-26 06:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found