Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Delving the regexp underdark -- how to understand \G's behavior, or how to loop through a regular expression

by salvadors (Pilgrim)
on Feb 02, 2001 at 21:11 UTC ( [id://56030]=note: print w/replies, xml ) Need Help??


in reply to Delving the regexp underdark -- how to understand \G's behavior, or how to loop through a regular expression

You need to tell Perl that your string is multiple lines of text, using the "m" modifier:

my $text = qq{foo this is one example bar this is a line I don't care about foo here's another keeper! bar foo yet another bar and a line to reject}; foreach ($text =~ /^foo(.*)bar$/gm) { manipulate($_); } sub manipulate { print $_, "\n"; }
(Note this assumes that foo and bar are going to be at the start and end of lines. It wasn't clear from your description whether "and a foo line to bar reject" should return nothing or "line to". Feel free to remove the ^ and $ to achieve the 2nd...)

Tony

  • Comment on Re: Delving the regexp underdark -- how to understand \G's behavior, or how to loop through a regular expression
  • Download Code

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-04-19 09:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found