MrDoney has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am trying to create a regex that prints out the data contained between two text flags- "Twist 1" and "Twist 2" The data looks like this: Twist 0\n ... (load of uninteresting data) Twist 1\n ... (load of interesting data) Twist 2\n ...(more unintersting data) I have been trying to use the following code: perl -n -e 'while(m/Twist 1(.*)/Twist 2/g){print "$1";}' testresults where testresults is the name of my text file. Unfortunately, I get no results at all from this regex. What am I doing wrong? Is it something to do with having newlines in the text? Thanks Michael

Replies are listed 'Best First'.
Re: Pattern Matching Problem
by davorg (Chancellor) on Jun 26, 2000 at 14:38 UTC
      This is why I frequent perlmonks... I always learn stuff.

      I had one issue with your method, it printed the Twist flags. So I tried it this way:

      perl -n -we "print if /Twist 1/ .. /Twist 2/ and not /Twist \d/" test. +txt
      Which stripped out the flags too. However, I would bet there is a better way to do it that I am not able to think of at the moment.
        Hmm, how about: perl -n -we "print if s/Twist 1// .. s/Twist 2// && ! /Twist \d/" text.txt Untested, as I'm still sure what your data set is.
Re: Pattern Matching Problem
by le (Friar) on Jun 26, 2000 at 14:20 UTC
    I would try the following:
    perl -e 'while(<>) {print $1 if /Twist 1(.+)Twist 2/g;}' < testresults
    but this is untested.
RE: Pattern Matching Problem
by mrmick (Curate) on Jun 26, 2000 at 17:34 UTC
    How about:
    perl -n -e 'if(/Twist 2$/){s/Twist 2//;print;} testresults'
    Because you are going through the file line-by-line (default behaviour when assuming a while loop around the program), just test for the 'Twist 2' string at the end of the line.
Re: Pattern Matching Problem
by t0mas (Priest) on Jun 26, 2000 at 18:12 UTC
    You'll want to use m//mg to match on a multi-line string (and undef $/ or something :), but the .. as davorg is a simpler way of doing it.

    /brother t0mas
Re: Pattern Matching Problem
by MrDoney (Initiate) on Jun 26, 2000 at 15:06 UTC
    Hey, it works!! What is the .. operator supposed to do? I've not seen it before.. Mike

      Look at the section on "Range Operators" in perldoc perlop.

      Basically, when used in a scalar context (like an if statement). It returns false until the left hand expression is true. It then returns true until the right hand expression becomes true.

      Hope this helps.

      --
      <http://www.dave.org.uk>

      European Perl Conference - Sept 22/24 2000
      <http://www.yapc.org/Europe/>