in reply to regex to remove space

To preserve the leading space and first word, use the \K token, (for keep), and the transliteration operator, tr, inside a substitution.
#!/usr/bin/perl use strict; use warnings; while (<DATA>) { s[^\s*\S+\K(.*)$][$1 =~ tr/ \t/,/rs]e; print; } __DATA__ I like this script I like this script
Output is:
I,like,this,script I,like,this,script

Replies are listed 'Best First'.
Re^2: regex to remove space
by graff (Chancellor) on Dec 16, 2015 at 04:42 UTC
    I think there's an easier way:
    s/(?<=\S) +/,/g;
    That replaces one or more contiguous spaces with comma throughout the string, but only if there's a non-space character preceding (positive look-behind).

      You sir deserve more upvotes! Lol, thank you so much!