In the "Advanced Regular Expresssion" chapter in Mastering Perl, I have a broken example for which I can't figure out a nice fix. The example is perhaps trying to be too clever for its own good, but maybe someone can fix it for me. There could be a free copy of the book in it for working fixes. :)
In the section talking about lookarounds, I wanted to use a negative lookbehind to implement a commifying routine for numbers with fractional portions. The point was to use a negative lookbehind because that was the topic.
I stupidly did this:
$_ = '$1234.5678';
s/(?<!\.\d)(?<=\d)(?=(?:\d\d\d)+\b)/,/g; # $1,234.5678
The (?<!\.\d)
asserts that the bit before the (?=(?:\d\d\d)+\b)
is not a decimal point and a digit.
The stupid thing is not trying hard enough to break it. By adding another digit to the end, there is now a group of three digits not preceded by a decimal point and a digit:
$_ = '$1234.56789';
s/(?<!\.\d)(?<=\d)(?=(?:\d\d\d)+\b)/,/g; # $1,234.56,789
If lookbehinds could be variable width in Perl, this would have been really easy. But they can't.
Note that it's easy to do this without a negative lookbehind, but that's not the point of the example. Is there a way to salvage this example?
\b
to(?:\d\d\d)+\b)
when you posted this, so I added it. (I checked, and it is there in the book.) That's just a distraction, though; it has nothing to do with the lookbehind problem. – Breslau(?<!)
. I'm not looking for ways around it. – Absorber