Hi there, I am far from understanding perl - but I have to get along with it. In other words, I'm completely new to this. :-(

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

$restofline = "One fish two fish red fish blue fish";
To split the words "one", "two", "red" and "blue" I tried a simple match:
while($restofline =~ m/(.*)fish(.*)/) { $beginofline = $1; $restofline = $2; #.... do some mighty magic }
Sooo ....
What happens is that it always finds the LAST match instead of the first! The Problem seems to be the "(.*)" to the left of the keyword. As soon as I try
$restofline =~ m/fish(.*)/
it works fine. But how do I get the text to the left of the matched keyword then?

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


In reply to match last element instead of first?? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.