First of all, $& will not have the whole value of line, because it holds only the string matched by the pattern match - by the last successful pattern match to be precise - this means that even if the current string doesn't match with the pattern match the value of $& will not be changed. The string value held in $& will be updated to the current successful pattern matched as soon as the pattern matches the string.

Be warned that performance is compromised if the $& variable is used any where in the program and that this variable is a read-only variable.

Secondly, since you've not provided an example of the lines that you're reading from to perform this match it is a bit unwieldy to try to replicate

$line=~/.*\sid=\"(.*)\"\slo=\"(.*)\"\sto=\"(.*)\"\srb=.*$/;
while you only need $2 then why do you wanna worry about $& in the first place?

Does your code take care of when the lines don't match with the above pattern or it generates an uninitialized variable warning when printing $2?

#maybe try something like print "$2\n\n" if $2;


Excellence is an Endeavor of Persistence. A Year-Old Monk :D .

In reply to Re: regular expression. by biohisham
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.