in reply to Extracting substrings

with look ahead and look behind

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $string = 'WhateverFooBlahBarMoreFooStuffBarMore'; my @words = $string =~ / (?<=Foo) (.*?) (?=Bar) /gx; print Dumper \@words;
Output: $VAR1 = [ 'Blah', 'Stuff' ];
hth,

PooLpi

'Ebry haffa hoe hab im tik a bush'. Jamaican proverb

Replies are listed 'Best First'.
Re^2: Extracting substrings
by ambrus (Abbot) on Feb 18, 2008 at 15:25 UTC

    Meh, that's too little unneeded complication. Why not use the new (in perl 5.10.0) feature Keep too: print "<<$_>>\n" for "WhateverFooBlahBarMoreFooStuffBarMore" =~ /Foo\K.*?(?=Bar)/gx;

    Really, if you add a capture around the word anyway, why do you need the lookaheads at all? print "<<$_>>\n" for "WhateverFooBlahBarMoreFooStuffBarMore" =~ /Foo(.*?)Bar/gx;