Stay away from this $& stuff as that will slow all regexes down. And its very seldom needed. If you just need to check if: lo="XYZZY" is on the line, the regex is easier than below. But this a general name="value" solution.
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; while (<DATA>) { next unless (/^<dsk1/); my %hash = m/(\w+)\s*=\s*"(.*?)"/g; next unless keys %hash; #skip blank hashes (no pairs) print "name=value pairs:\n"; foreach my $name (sort keys %hash) { print " $name=>$hash{$name}\n"; } print "\n"; } =prints: name=value pairs: id=>123 lo=>1 rb=>This is a long rb. to=>abc name=value pairs: id=>456 lo=>2 rb=>Short rb to=>def name=value pairs: id=>789 lo=>3 rb=>Medium long rb to=>ghi name=value pairs: id=>987 lo=>6 rb=>This should be a match to=>mno name=value pairs: id=>FFF lo=>7 rb=>This is a very, very, very long are-bee. to=>pqr =cut __DATA__ <dsk1 line1 id="123" lo="1" to="abc" rb="This is a long rb." <dsk1 line2 id="456" lo="2" to="def" rb="Short rb" <dsk1 line3 id="789" lo="3" to="ghi" rb="Medium long rb" <dsk2 line4 <dsk2 line5 id="555" lo="5" to="jkl" rb="should not match" <dsk1 Line6 id="987" lo="6" to="mno" rb="This should be a match" <dsk1 line7 id="FFF" lo="7" to="pqr" rb="This is a very, very, very lo +ng are-bee."
In reply to Re: regular expression.
by Marshall
in thread regular expression.
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |