Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
So here's the problem I'm going to town with: I'd like to parse a line of text for a keyword, and then "split" the line at that place. I need the text prior to the match also, and the keywords may occur more than once.
To stick with the perlfaq example, let's say my text is
To split the words "one", "two", "red" and "blue" I tried a simple match:$restofline = "One fish two fish red fish blue fish";
Sooo ....while($restofline =~ m/(.*)fish(.*)/) { $beginofline = $1; $restofline = $2; #.... do some mighty magic }
it works fine. But how do I get the text to the left of the matched keyword then?$restofline =~ m/fish(.*)/
I tried several things like the g modifier, {1} and + match quantifiers, but to no avail. It either works like finding the last match or it doesn't work at all.
Can you guys please tell me what I did wrong and - even more important - how to do that right?
Thanks in advance, Faltblatt
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: match last element instead of first??
by jettero (Monsignor) on Oct 17, 2007 at 17:07 UTC | |
by moritz (Cardinal) on Oct 17, 2007 at 17:11 UTC | |
by Corion (Patriarch) on Oct 17, 2007 at 17:30 UTC | |
|
Re: match last element instead of first??
by kyle (Abbot) on Oct 17, 2007 at 17:18 UTC | |
|
Re: match last element instead of first??
by amarquis (Curate) on Oct 17, 2007 at 17:31 UTC | |
|
Re: match last element instead of first??
by johngg (Canon) on Oct 17, 2007 at 17:50 UTC | |
|
Re: match last element instead of first??
by Anonymous Monk on Oct 18, 2007 at 09:19 UTC | |
|
Re: match last element instead of first??
by naikonta (Curate) on Oct 18, 2007 at 06:49 UTC |