On Jun 16, bzzt said:

>I'm trying to match a patern in a string but I want to do it a line at a
>time. Is there an easier way than this :

>while ($a =~ m/(.+?)\n/g ) {
>   if ($1 =~ /whatever/g) {
>    print "$1";
>   }

Your regex, /(.+?)\n/, is unnecessarily complex.  The ? modifier on .+
isn't helping you any, and if you just make the regex /(.+)/g, it will
work just fine.

That code won't work properly unless 'whatever' is a regex that captures
something to $1.  If you wanted to print the entire LINE if it matched,
you'd have to do:

  while ($a =~ /(.+)/g) {
    my $str = $1;
    if ($str =~ /.../) { print $str }
  }

Someone suggested splitting $a into an array, and matching on the elements
of the array.  Another approach (although I don't vouch for its speed)
would be:

  while ($a =~ /^(.*pattern.*)$/mg) {
    print $1;
  }

The ^ and $ anchors change in meaning when used with the /m modifier.  Now
they mean beginning of line and end of line, paying attention to embedded
newlines in $a.

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
CPAN ID: PINYAN    [Need a programmer?  If you like my work, let me know.]
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to