I'm not sure what you are trying to accomplish. The natural thing to think about with x=123 y=456 type pairs is a hash. Below I show how to generate that hash with match global. You could check if the "lo" key is defined or not.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.