in reply to Re: Regex split string into n-length
in thread Regex split string into n-length

use 5.009005; $string =~ s/\w{3}\K/-/g;

Replies are listed 'Best First'.
Re^3: Regex split string into n-length (\K 5.010)
by kcott (Archbishop) on Sep 10, 2013 at 07:52 UTC

    That adds a terminal "-" whenever the string's length is a multiple of 3:

    $ perl -Mstrict -Mwarnings -E ' my @list = qw{12 123 1234 12345 123456 1234567 234 2345 23456}; for (@list) { s/\w{3}\K/-/g; say; } ' 12 123- 123-4 123-45 123-456- 123-456-7 234- 234-5 234-56

    -- Ken

      Hi Ken,

      You are absolutly right. Thank you for showing this error.

      Look at my update.

      McA

Re^3: Regex split string into n-length (\K 5.010)
by McA (Priest) on Sep 10, 2013 at 07:52 UTC

    \K, these are the little pearls in Perl. ++

    Thank you

    McA