in reply to how to get context between two flag
If your ending tag is a constant string, e.g. 'end' then the easiest way is to just set your end-of-record marker to that value using the Perl variable $/. You can learn more about $/ in perlvar. That way you will be sure to read in the entire run from 'start' to 'end' in a single gulp and the new lines won't cause you any trouble if 'start' is on one line and 'end' is on another. For example,
use strict; use warnings; local $/='end'; my @aFound; while (my $line=<DATA>) { # normally we would chomp to get rid of 'end' # but this may not be a good idea if the file ends in # junk outside of start ... end. # chomp $line; # make sure we really have a match just in case there is # an 'end' without a preceding "start"! # also use the s modifier at the end of your regex so that # . matches "\n" # see http://perldoc.perl.org/perlre.html#Modifiers # for further information next unless ($line =~ /\s+start\s+(.*)end\z/s); # store extracted string for later use push @aFound, $1; } # do something with the text between start...end # you'll want to change this: # your post looks like you would like to further separate # each word on a separate line, but for now, lets just # print out the run of characters between start...end. print join("\n", @aFound), "\n"; __DATA__ asdasd start asdasd asdasdasd asdasdas end asdasdas adasdas start as asdas dasdasdad asdasddas end qweqwe asdasd start asdsadsdasddasds sdasdas asdasdasdasd asdasdsa asdasd asdasdasd end here is some trailing garbage
which outputs
asdasd asdasdasd asdasdas as asdas dasdasdad asdasddas asdsadsdasddasds sdasdas asdasdasdasd asdasdsa asdasd asdasdasd
Best, beth
Update Fixed bug (missing s modifier on regex).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: how to get context between two flag
by Marshall (Canon) on Aug 07, 2009 at 07:47 UTC |