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

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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2017-07-20 20:58 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (315 votes). Check out past polls.