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

$nums = "1,2,3,4,5,6"; $nums =~ s/\D/ /; print "$nums\n";
output: "1 2,3,4,5,6" why is that and not: "1 2 3 4 5 6" ? why only the first comma ? and if I change $nums
$nums = "1_2#3!4.5/6"; $nums =~ s/\D/ /; print "$nums\n";
it's the same: "1 2#3!4.5/6" whyyyyy ?

Replies are listed 'Best First'.
Re: Getting Trouble with REGEXP
by toolic (Bishop) on Jun 22, 2009 at 18:23 UTC
    Use the global modifier 'g':
    $nums =~ s/\D/ /g;

    Search for 'modifier' in perlop.

Re: Getting Trouble with REGEXP
by ikegami (Patriarch) on Jun 22, 2009 at 18:23 UTC
    It finds a non-digit and replaces it with a space. If you want it to do it repeatedly, use /g.
    $nums =~ s/\D/ /g;

    See s/PATTERN/REPLACEMENT/... under "Regexp Quote-Like Operators" in perlop. ( er, the documentation of /g was moved to perlre in 5.10 )

Re: Getting Trouble with REGEXP
by zwon (Abbot) on Jun 22, 2009 at 18:24 UTC
    $nums =~ s/\D/ /g;
Re: Getting Trouble with REGEXP
by linuxer (Curate) on Jun 22, 2009 at 19:42 UTC