You need to specify that the string you look for should not appear in
the part you try to extract, meaning instead of .*? you should be
looking for (not abc)*? In perl, we have the negative lookahed for
that: (?!...): m/abc((.(?!abc))*?)xyz/

However, this would fail if you have a string abc abcabc toto toto...
because it's a "zero-length" test and therefore it would check the
first character after the abc abc and see that it's 1: a, and 2: is
followed by a "bc " which is OK. So we need to place the negative
lookahead on both sides of the character:
m/abc(((?!abc).(?!abc))*?)xyz/

Not a very nice looking regex, but does the trick:

$st = "abc abcabc toto toto xyz xyz xyz";
print $1 if $st =~ m/abc(((?!abc).(?!abc))*?)xyz/

PS C:\> perl .\test.pl
 toto toto


On 27 April 2010 10:52, HolyNoob <nhagiaubungbu2...@gmail.com> wrote:
> Hi,
>
> I'm trying to make a regexp to match the last appearance of a word (lets say
> 'abc') until the first appearance of another word (for ex: 'xyz'), and I
> still cannot do it.
>
> For example: with a string like this "abc abc abc toto toto xyz xyz xyz" ,
> which regexp I have to use to get "abc toto toto xyz".
> I can extract this "abc abc abc toto toto xyz" with   m/abc(.*?)xyz/
> but still got a problem to have exactly what I need.
>
> Would you guys please help me?
>
> Thank you very much
>



-- 
Erez

Support the New Israeli Fund - http://www.nif.org/

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to