Hi.
I'm more of a newbie Perl programmer (1st post here also).
Today I (actually somebody else, but I'm posting this here now ;)) stumbled across some strange problem with regular expressions.

The code is simple:
$_ = '511111'; print if /^(\d)\d{\1}$/;
The first digit of the string in $_ indicates the number of digits following.
(For example, 233 should match, 42143 also, but 324 should not.)
However, the code above does not work.
We know how to work around this problem, the problem is NOT to find a solution for this. We simply want to know WHY this does not work.
It seems that this is not something trivial.


Furthermore, we were wondering:
If the regexp above would work, what if the first TWO or more digits would indicate how many digits follow? (meaning that >= 10 digits follow)
How could you stuff that into one regexp? (it's also just a theoretical question, but an interesting one IMHO).

Thanks in advance.

In reply to RegExp madness by palandir

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.