Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Getting Trouble with REGEXP

by Oromis92 (Sexton)
on Jun 22, 2009 at 18:15 UTC ( #773715=perlquestion: print w/ replies, xml ) Need Help??
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 ?

Comment on Getting Trouble with REGEXP
Select or Download Code
Re: Getting Trouble with REGEXP
by toolic (Chancellor) 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 (Pope) 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 (Monsignor) on Jun 22, 2009 at 18:24 UTC
    $nums =~ s/\D/ /g;
Re: Getting Trouble with REGEXP
by linuxer (Deacon) on Jun 22, 2009 at 19:42 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://773715]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2014-12-26 02:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (164 votes), past polls