Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Need a one liner

by Ananda (Pilgrim)
on Jul 17, 2004 at 10:21 UTC ( #375220=perlquestion: print w/ replies, xml ) Need Help??
Ananda has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks, I need a oneliner for the below regex. I havent been able to search for the solution due to time constraint.

$a =~ s/\W//g;

$a =~ s/_//g;

Thanks for all the Help.

Comment on Need a one liner
Replies are listed 'Best First'.
Re: Need a one liner
by beable (Friar) on Jul 17, 2004 at 10:36 UTC
    #!/usr/bin/perl use strict; use warnings; my $a = '2323jjf ("$("&&"&$__\\/'; my $b = $a; $a =~ s/\W//g; $a =~ s/_//g; $b =~ s/\W|_//g; print "a = $a\n"; print "b = $b\n"; __END__
    Or, in one line:
    perl -pe 's/\W|_//g;'
      Or, in the sense of TMTOWTDI:  s/[\W_]//g
      How can a "." character be allowed in the string.
        #!/usr/bin/perl use strict; use warnings; my $a = my $b = my $c = join '', map(chr, 32 .. 126); print "a = $a\n"; $a =~ s/\W//g; $a =~ s/_//g; $b =~ s/\W|_//g; # allow letters, numbers, and dots $c =~ s/[^a-z0-9.]//gi; print "a = $a\n"; print "b = $b\n"; print "c = $c\n"; __END__
        You should take a look at perlre when you get a chance.
Re: Need a one liner
by crabbdean (Pilgrim) on Jul 17, 2004 at 10:31 UTC
      $a =~ r/\W_//g;

      There is no /g modifier for the tr/// operator. The transliteration operator also does not use regexp-like predefined character classes, such as \W. You probably meant something like:

      $string =~ tr/a-zA-Z0-9.//cd;

      This says to take the complement of the list created by the ranges a-z, A-Z, and 0-9, and delete anything not found among that range. Note, I didn't specifically deal with the underscore character, because with this method, the fact that I didn't include it is enough to get it deleted. Think of the list of things on the left of the tr/// operator as the list of what to keep, because we used the /c modifier. Everything else gets deleted, because we used the /d modifier.

      Updated: Added the '.' character to the "keep these" character list to accommodate the fact tht the OP revised his question later in the thread asking to also preserve the dot character.


Re: Need a one liner
by ihb (Deacon) on Jul 18, 2004 at 18:42 UTC

    This is what charclasses are for.


    Read more about them in perlrequick, perlretut, perlre, and perlreref (at for instance

    (Using alternation for this is really awful and not just style-wise.)


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (11)
As of 2015-11-30 11:33 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (769 votes), past polls