in reply to Re: Lost in Perl - text file searching and printing
in thread Lost in Perl - text file searching and printing

Thanks for your replies! I am getting closer to understanding this... a bit of clarification: Yes, the text file can contain lines that do not contain the word "it". So, I need it to look for lines that match both the punctuation, and then containing the word "it", I have it selecting the correct text now based on the previous reply and some modification, but can't figure out how to only print the text AFTER that word... any advise? (I hope to understand this stuff yet... Perl regular expressions are a bit cryptic!!! Thanks again! Eric
  • Comment on Re^2: Lost in Perl - text file searching and printing

Replies are listed 'Best First'.
Re^3: Lost in Perl - text file searching and printing
by zentara (Cardinal) on Jul 24, 2011 at 11:37 UTC
    Well here is a 2 step solution. It still may have a problem if you encounter lines with 2 or more 'it' in them. Its called regex greediness. See greediness . I leave you some work to fix the possible greediness issue. :-)
    #!/usr/bin/perl use strict; use warnings; while (<DATA>) { if ( $_ =~ m/([?.,]$)/){ # print " matching first criteria $_"; #now check for 'it', using capture parenthesis #for the text before and after 'it' if ( $_ =~ m/(.*)it(.*)/ ){ print "$2\n"; # the second capture parenthesis } }; } __DATA__ 4 score, and 7 years ago? our ? forefathers came, upon it -- a continent. it was it was it ? foo? .bar bazz __END__

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh