in reply to Parsing multiline string line by line

Or, you can save a little memory like this:
use strict; use warnings; my $str = "First line\nsecond line\netc. lines here"; while($str =~ /([^\n]+)\n?/g){ print "LINE: $1\n"; }
but probably you need to work on it if you need to process empty lines as well.

edit: deleted /s modifier.

Replies are listed 'Best First'.
Re^2: Parsing multiline string line by line
by metaperl (Curate) on Feb 19, 2009 at 14:47 UTC
    Why did you use the /s modifier? That only changes '.' from matching anything but carriage return to also matching it. I dont think it is doing anything useful there.
      yes you are right. somehow I always use s whenever I think about new lines. edited. thanks.
Re^2: Parsing multiline string line by line
by flamey (Scribe) on Feb 19, 2009 at 14:21 UTC
    i was sure there would be some way of doing it with regex, but i don't quite understand what's happening here: first iteration it will match a string of non-newline chars (first line), but how does it know to continue on the "second (etc.) line" on the rest of the iterations of the while loop?

    thanks for reply!!
      Perhaps it's a truism that for any computational process, there is a regex of arbitrary complexity which will solve it.
      check this, it is explained in the global matching section.