Tested and verified. Apparently, the sections stay static, and if the string becomes shorter, the length of the string, plus one or two characters, overwrite the $<digit> variables. If you regexp again, though, they get reset as they should be. What I tried:
use re 'debug'; use strict; my (@rere,$rere); $_="hello world"; @rere = /(\w+)\s+(\w+)/; $rere = "(".join(",",@rere).")"; print "\$1 = $1, \$2 = $2, \$rere = $rere\n"; $_="foo bar"; print "\$1 = $1, \$2 = $2, \$rere = $rere\n"; @rere = /(\w+)\s+(\w+)/; $rere = "(".join(",",@rere).")"; print "\$1 = $1, \$2 = $2, \$rere = $rere\n";
with the following output:
Compiling REx `(\w+)\s+(\w+)' size 15 first at 4 1: OPEN1(3) 3: PLUS(5) 4: ALNUM(0) 5: CLOSE1(7) 7: PLUS(9) 8: SPACE(0) 9: OPEN2(11) 11: PLUS(13) 12: ALNUM(0) 13: CLOSE2(15) 15: END(0) stclass `ALNUM' plus minlen 3 Compiling REx `(\w+)\s+(\w+)' size 15 first at 4 1: OPEN1(3) 3: PLUS(5) 4: ALNUM(0) 5: CLOSE1(7) 7: PLUS(9) 8: SPACE(0) 9: OPEN2(11) 11: PLUS(13) 12: ALNUM(0) 13: CLOSE2(15) 15: END(0) stclass `ALNUM' plus minlen 3 Matching REx `(\w+)\s+(\w+)' against `hello world' [...] Match successful! $1 = hello, $2 = world, $rere = (hello,world) $1 = foo b, $2 = r rld, $rere = (hello,world) Matching REx `(\w+)\s+(\w+)' against `foo bar' [...] Match successful! $1 = foo, $2 = bar, $rere = (foo,bar) Freeing REx: `(\w+)\s+(\w+)' Freeing REx: `(\w+)\s+(\w+)'

As for the null character, that might be a byproduct of C(++) string processing -- for those who may not know, in C(++) strings are terminated with a null.


In reply to Re: Re: Another regex variable puzzle by premchai21
in thread Another regex variable puzzle by Rudif

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.