wog is right. My mistake was to put the right anchor outside the zero width look-ahead assertion. The correct substitution code is:

 s/(\d{1,3}?)(?=(\d{3})+$)/$1_/g;

The lookahead makes sure that the number of digits before each underscore we insert is a multiple of 3

The lookahead: (?=(\d{3})+$)
I needed an extra set of parenthesis to fool Perl because the regexp parser barks if there is two quantifiers in a row, which is perfectly legitimate here.

There is a general lesson to be learned here: unchecked idiotism are for idiots. So much for me :)

When dealing with new material (here regexp assertion that I have not used much), one must learn to reassess idiotisms that may not work in a new larger context. Here, I used the idotism: force the match to the end of the string => add a $ at the the very end of the regexp. It did not work here because I wanted the lookahead to match to the end of the string.

Compare the previous code with the easy way

-- stefp


In reply to Re: Re: bug in regexp engine? by stefp
in thread bug in regexp engine? by stefp

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.