hankcoder has asked for the wisdom of the Perl Monks concerning the following question:

Hi I'm new here. I had search days on the task I want to perform but I don't know what exactly it is called and can't find it so I have to ask for help.

Task: To split a string into equal fixed characters using regex

Problem: Modify current regex so result would appear in reverse order as "111-111-1" instead "1-111-111".

I prefer to use regex, not to use reverse function.

I couldn't remember where exactly I found below regex before, it works perfectly. Only now I want to make another version out of it so I can perform as I mentioned above.

$line =~ s/(?<=\w)(?=(?:\w\w\w)+\b)/-/g;
--- Perl sample codes ----- my (@list) = ( "12", "123", "1234", "12345", "123456", "1234567", "234", "2345", "23456" ); foreach $line (@list) { $line =~ s/(?<=\w)(?=(?:\w\w\w)+\b)/-/g; print "$line<br>"; }
--- results of above code 12 123 1-234 12-345 123-456 1-234-567 234 2-345 23-456

I want it appear as:

123-4 123-45 123-456 123-456-7

Many thanks in advance.

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

    G'day hankcoder,

    Welcome to the monastery.

    "s/(\w{3})\B/$1-/g" does what you want:

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

    -- Ken

Re: Regex split string into n-length
by hdb (Monsignor) on Sep 10, 2013 at 08:18 UTC

    Yet another way of doing it:

    join '-', /\w{1,3}/g
Re: Regex split string into n-length
by Laurent_R (Canon) on Sep 10, 2013 at 09:07 UTC

    Or possibly:

    say join "-", split /\w{1,3}\K/ for @list;

      ++ Interesting use of  \K in split. Learned something today!

Re: Regex split string into n-length
by johngg (Canon) on Sep 10, 2013 at 10:51 UTC

    You could also use unpack.

    $ perl -E ' say for map { join q{-}, unpack q{(a3)*}, $_ } qw{ 12 123 1234 12345 123456 1234567 };' 12 123 123-4 123-45 123-456 123-456-7 $

    I hope this is helpful.

    Cheers,

    JohnGG

Re: Regex split string into n-length
by McA (Priest) on Sep 10, 2013 at 07:07 UTC
    $string =~ s/(\w\w\w)/$1-/g;

    UPDATE: As Ken pointed out correctly, this solution is wrong as it puts an additional - in a case like '123456'.

    So, putting a negative lookahead to the pattern should do the trick:

    $string =~ s/(\w\w\w)(?!$)/$1-/g;

    McA

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

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

        Thank you

        McA

Re: Regex split string into n-length
by hankcoder (Scribe) on Sep 10, 2013 at 17:28 UTC

    Thank you so much guys. I didn't notice I got such many helps here. ;)

    Just share, I got another working regex from perlguru, I changed \d to \w.

    $line =~ s/(\w{3}(?!$))/$1-/xg;

    By the way, is there a major processing speed to concern since there are more than one regex to get the same results?

    such as:
    $string =~ s/(\w\w\w)(?!$)/$1-/g; from McA s/(\w{3})\B/$1-/g from Ken