I'm trying to find groups of repeated digits in a number, e.g. 12334555611
becomes (1 2 33 4 555 6 11)
.
This works:
$n.comb(/ 0+ | 1+ | 2+ | 3+ | 4+ | 5+ | 6+ | 7+ | 8+ | 9+ /)
but is not very elegant.
Is there a better way to do this?
'12334555611'.comb(/\d+ % <same>/)
Please check the answer of the first task of Perl Weekly Challenge
You may use
$n.comb(/(.) $0*/)
The (.)
creates a capturing group and captures any char into Group 1, then there is a backreference to Group 1 that is $0
in Perl6 regex. The *
quantifier matches zero or more occurrences of the same char as in Group 1.
Replace the .
with \d
to match any digit if you need to only match repeated digits.
See a Perl6 demo online.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With