Line1 : Dit is de eerste regel Line2 : Dit is de tweede regel Line3 : Dit is de derde regel Line4 : Dit is de vierde regel
Use a non-greedy match:
#!/usr/bin/perl use strict; use warnings; local $/=undef; my $contents=; my ($thirdline) = $contents =~ m/Line3 : (.*?)\n/s; print $thirdline."\n"; __DATA__ Line1 : Dit is de eerste regel Line2 : Dit is de tweede regel Line3 : Dit is de derde regel Line4 : Dit is de vierde regelOutput:
Dit is de derde regelOr you could match against a negated character class:
my ($thirdline) = $contents =~ m/Line3 : ([^\n]+)/s;
You may view the original node and the consideration vote tally.
use strict;
my $string = join '', ;
if($string =~ /^Line3 : (.*)/m) {
print "$1\n";
}
__DATA__
Line1 : Dit is de eerste regel
Line2 : Dit is de tweede regel
Line3 : Dit is de derde regel
Line4 : Dit is de vierde regel
The /m modifier is necessary to have the ^ anchor match the beginning of any line in a multi-line string. The greedy .* will then match anything until the end of that line.
Had you used the /s modifier, the greedy .* would have matched newlines as well and therefore gobbled up everything until the end of the multi-line string.
You may view the original node and the consideration vote tally.
Use '?' to get a nongreedy match up to the first newline:
$data =~ m/Line3 : (.*?)\n/s;
You can also use a greedy match with a negated character class:
$data =~ m/Line3 : ([^\n]*)/s;
My test code follows:
use warnings; use strict; my $data = join( '', ); print "[$data]\n\n"; $data =~ m/Line3 : (.*?)\n/s or die; print "match: [$1]\n"; $data =~ m/Line3 : ([^\n]*)/s or die; print "match: [$1]\n"; __DATA__ Line1 : Dit is de eerste regel Line2 : Dit is de tweede regel Line3 : Dit is de derde regel Line4 : Dit is de vierde regel
Some sample code with "this is what I get", and "this is what I want" would help understand where you are having a problem. The following should be a good starting point, if not the stimulus for a D'oh moment :).
use strict;
use warnings;
my $lines = do {local $/; };
my ($line3) = $lines =~ /Line 3 : (.*?)\n/;
print ">$line3<";
__DATA__
Line 1 : Dit is de eerste regel
Line 2 : Dit is de tweede regel
Line 3 : Dit is de derde regel
Line 4 : Dit is de vierde regel
Prints:
>Dit is de derde regel<
You may view the original node and the consideration vote tally.
I have slurped this into a scalar called $contents using open and my $contents = join('',That works, of course .. just wanted to share my immediate thought of this node: node 287647) . This all works right.
The task appears to be line-oriented, so it would make sense to stick with line-oriented handling of the data. Depending on what else might need to be done with the file contents in the same script (whether you need to do things with other lines besides "Line 3"), you could either read the whole file into an array of lines and use grep on the array, or else use grep directly on the line-oriented file-read operator:
# load file into an array of lines, and use "Line 3": my @lines =; my ( $keeper ) = grep /^Line 3 : /, @lines; # or just get "Line 3" from the file, and skip the rest: #my ($keeper) = grep /^Line 3 : /, ; # (update: added parens around $keeper, as per Aristotle's correction) # either way, remove the unwanted content from the kept line: $keeper =~ s/Line 3 : //;
You may view the original node and the consideration vote tally.
Careful, youre invoking grep in scalar context. $keeper will only contain the count of matches. This has to be written with a parenthesised [doc://my], like so:
my ( $keeper ) = grep /^Line 3 : /, @lines;
However, that always goes through the entire data, regardless of where the match is found. A better way would be [doc://List::Util]s first; with which the context does not matter either:
use List::Util qw( first );
my $keeper = first { /^Line 3 : / } @lines;
Makeshifts last the longest.
perlmonks.org content © perlmonks.org and Aristotle, bobf, davidrw, graff, GrandFather, jkva, NodeReaper, saintmike, tirwhan
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03