Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
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 wandering the Monastery: (10)
As of 2014-07-25 23:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls