Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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.
Ananda

Comment on Need a one liner
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.


      Dave

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;'
      How can a "." character be allowed in the string.
      Ananda
        #!/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.
      Or, in the sense of TMTOWTDI:  s/[\W_]//g
Re: Need a one liner
by ihb (Deacon) on Jul 18, 2004 at 18:42 UTC

    This is what charclasses are for.

      s/[\W_]//g

    Read more about them in perlrequick, perlretut, perlre, and perlreref (at for instance www.perldoc.com).

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

    ihb

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2014-10-02 02:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (44 votes), past polls