在 Mastering Perl 的“高级正则表达式”一章中,我有一个坏的例子,我想不出一个很好的解决办法。这个例子可能是为了自己的利益而试图过于聪明,但也许有人可以为我解决它。里面可能有这本书的免费副本,用于修复工作。 :)
在讨论环视的部分中,我想使用否定的环视来实现带有小数部分的数字的 commifying 例程。关键是要使用负面的回顾,因为那是主题。
我愚蠢地这样做了:
$_ = '$1234.5678';
s/(?<!\.\d)(?<=\d)(?=(?:\d\d\d)+\b)/,/g; # $1,234.5678
(?<!\.\d)
断言 (?=(?:\d\d\d)+\b)
之前的位不是小数点和数字。愚蠢的事情是没有努力去打破它。通过在末尾添加另一个数字,现在有一组三个数字,前面没有小数点和一个数字:
$_ = '$1234.56789';
s/(?<!\.\d)(?<=\d)(?=(?:\d\d\d)+\b)/,/g; # $1,234.56,789
如果在 Perl 中lookbehinds 可以是可变宽度,这将非常容易。但他们不能。
请注意,在没有负面回顾的情况下很容易做到这一点,但这不是示例的重点。有没有办法挽救这个例子?
最佳答案
我认为没有某种形式的可变宽度后视是不可能的。在 5.10 中添加的 \K
断言提供了一种伪造可变宽度正向后视的方法。我们真正需要的是可变宽度的负后视,但只要有一点创造力和很多丑陋,我们就可以让它发挥作用:
use 5.010;
$_ = '$1234567890.123456789';
s/(?<!\.)(?:\b|\G)\d+?\K(?=(?:\d\d\d)+\b)/,/g;
say; # $1,234,567,890.123456789
如果曾经有一种模式要求使用
/x
表示法,那就是这个:s/
(?<!\.) # Negative look-behind assertion; we don't want to match
# digits that come after the decimal point.
(?: # Begin a non-capturing group; the contents anchor the \d
# which follows so that the assertion above is applied at
# the correct position.
\b # Either a word boundary (the beginning of the number)...
| # or (because \b won't match at subsequent positions where
# a comma should go)...
\G # the position where the previous match left off.
) # End anchor grouping
\d+? # One or more digits, non-greedily so the match proceeds
# from left to right. A greedy match would proceed from
# right to left, the \G above wouldn't work, and only the
# rightmost comma would get placed.
\K # Keep the preceding stuff; used to fake variable-width
# look-behind
# <- This is what we match! (i.e. a position, no text)
(?= # Begin a positive look-ahead assertion
(?:\d\d\d)+ # A multiple of three digits (3, 6, 9, etc.)
\b # A word (digit) boundary to anchor the triples at the
# end of the number.
) # End positive look-ahead assertion.
/,/xg;
关于regex - 你能挽救我的负面回顾例子吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2330453/