Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: How do I extract the data that comes after a certain word?

by ikegami (Pope)
on Jun 18, 2012 at 19:55 UTC ( #976886=note: print w/ replies, xml ) Need Help??


in reply to How do I extract the data that comes after a certain word?

First, previous answers use `$1`, but I hate using global variables when it's not necessary. It's not necessary here.

Second, previous answers assume you don't want to capture newlines, but you didn't say anything of the kind.

Fix:

if (my ($match) = $s =~ /yesterday (.*?) after/s) { say $match; }

Finally, using the `?` greediness modifier can lead to surprises (especially if you use more than one in a single pattern). If given

yesterday foo yesterday bar after

the above regex will capture

foo yesterday bar

If you want

bar

use the following instead:

if (my ($match) = $s =~ /hello ((?:(?!yesterday).)*) yesterday/s) { say $match; }

(?:(?!STRING).) is to STRING as [^CHAR] is to CHAR.


Comment on Re: How do I extract the data that comes after a certain word?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2014-04-16 05:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (414 votes), past polls