Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
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.

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 contemplating the Monastery: (5)
As of 2016-09-25 09:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Extraterrestrials haven't visited the Earth yet because:







    Results (468 votes). Check out past polls.