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

Need a one liner

by Ananda (Pilgrim)
on Jul 17, 2004 at 10:21 UTC ( [id://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

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.
      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.
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 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
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-03-19 02:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found