in reply to REGEX detailed character replace

How do i change all dashes to commas except the first three

my $c = 0; s/-/$c++ < 3 ? '-' : ','/eg;
or
local our $c = 0; s/-(?(?{ $c++ < 3 })(?!))/,/g;
or
# As is, assumes at least 3 commas are found. my ($pre, $post) = /^((?:[^-]-){3})(.*)/s; $post =~ s/-/,/g; $_ = "$pre$post";
or
# As is, assumes at least 4 commas are found. my @parts = split(/-/, $_, -1); $_ = join(',', join('-', @parts[0..3]), @parts[4..$#parts]);

is there a way to do the first question along with removing white spaces, replacing pipes with commas (except first) all in one line

Yup

s/\|//; s/[|\s]+//g; my $c = 0; s/-/$c++ < 3 ? '-' : ','/eg;
s/\|//; s/\|/,/g; s/\s+//g; my $c = 0; s/-/$c++ < 3 ? '-' : ','/eg;

Update: Fixed rushed code as per reply.

Replies are listed 'Best First'.
Re^2: REGEX detailed character replace
by Anonymous Monk on Nov 12, 2008 at 09:14 UTC
    s/\|//; s/\s+//g; my $c = 0; s/-/$c++ < 3 ? '-' : ','/eg;#Here pipe is not replaced by comma..Its just removing all pipes
    s/^\|//; s/\s+//g; s/\|/\,/g my $c = 0; s/-/$c++ < 3 ? '-' : ','/eg;
    This should work fine
      We can also shrink further :)
      s/\|/$a++ < 1 ? '':','/eg;s/\s+//g;s/-/$c++ < 3 ? '-' : ','/eg;
        My goal wasn't to golf, but to provide a reasonable solution (as seen by my corruption of the OP's intent of "one line".) I don't think the extra complexity is warranted.