Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: Regex split string into n-length

by McA (Priest)
on Sep 10, 2013 at 07:07 UTC ( #1053177=note: print w/replies, xml ) Need Help??

in reply to Regex split string into n-length

$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;


Replies are listed 'Best First'.
Re^2: Regex split string into n-length (\K 5.010)
by Anonymous Monk on Sep 10, 2013 at 07:37 UTC
    use 5.009005; $string =~ s/\w{3}\K/-/g;

        Hi Ken,

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

        Look at my update.


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

      Thank you


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1053177]
[Cosmic37]: I'm afraid I don't know the answer to your map question...... what were the references too?
[corenth]: each reference was to a fairly small scaler.
[corenth]: I mean, array of scalers. usually triples.
[Cosmic37]: there is a FANN discussion group but seems to have gone quiet... http://leenissen. dk/fann/forum/ viewforum.php?f=18 &sid=1afc1fb3fa1a3 7f52b03e40d05cdaaa 3
[corenth]: Where are all the FANN fans?
[Cosmic37]: ok it went quiet because it moved - its actually quite active... https://sourceforg discussion/
[Cosmic37]: Therefore they are on sourceforge

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2018-02-20 18:33 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (274 votes). Check out past polls.