Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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.


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


use the following instead:

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

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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2016-10-27 11:20 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (360 votes). Check out past polls.