In my Camel the following appears on p 155, as an example of repeated global subsitution:
1 while s/(\d)(\d\d\d)(?!\d)/$1,$2)/;
I'm struggling to understand this. The purpose of the regex is to place commas before every third digit (counting backwards) - i.e. to format them nicely. And indeed, it does do this! But how?
At first I thought it might be a typo. If
! were a
:
1 while s/(\d)(\d\d\d)(?:\d)/$1,$2)/;
then I'd understand what was going on - we're just saving time by not capturing the third cluster. But then the regex doesn't work. Another way to do it is
1 while s/(\d)(\d\d\d)(?:,|$)/$1,$2/;
Which does work, and in a way I understand. But what I still don't get is this
?! business. I can't find anything in the Camel to explain it. Is it negation? But then why? And why doesn't
1 while s/(\d)(\d\d\d)(?:\D)/$1,$2/;
work?
I'd be very interested to know what's going on...
§
George Sherston
Edit: Changed title, on author's request. larsen
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.