Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Scurbbing a string

by moritz (Cardinal)
on Aug 24, 2007 at 21:03 UTC ( #634964=note: print w/replies, xml ) Need Help??

in reply to Scrubbing a string

Use regexes with char classes:

#!/usr/bin/perl use warnings; use strict; my $allowed = "pure"; my $re = quotemeta $allowed; my $str = "p u+r-e"; $str =~ s/[^$re]//g; print $str, "\n";

Replies are listed 'Best First'.
Re^2: Scurbbing a string
by FunkyMonk (Chancellor) on Aug 24, 2007 at 21:09 UTC
    I always believed that there wasn't interpolation inside a character class.


      I would have thought the same. But you could get around it by building your RE as a string and then putting that into the s///:
      my $allowed = "pure"; my $re = "[^\Q$allowed\E]"; my $str = "p uuu+tr-ed"; $str =~ s/$re//g; print $str, "\n";

      Caution: Contents may have been coded under pressure.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://634964]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2021-05-07 01:03 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (82 votes). Check out past polls.