Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elegant way to find repeated digits in Raku (née Perl 6)

Tags:

regex

raku

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?

like image 711
mscha Avatar asked Dec 04 '19 08:12

mscha


2 Answers

'12334555611'.comb(/\d+ % <same>/)

Please check the answer of the first task of Perl Weekly Challenge

like image 60
chenyf Avatar answered Oct 16 '22 22:10

chenyf


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.

like image 22
Wiktor Stribiżew Avatar answered Oct 16 '22 22:10

Wiktor Stribiżew