Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Getting Trouble with REGEXP

by Oromis92 (Sexton)
on Jun 22, 2009 at 18:15 UTC ( [id://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 ?

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-18 09:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found