regex - Filter out chars not in a set -
i trying filter strings pass through system, send out valid chars.
the following allowed.
a-z a-z "-" (hypen, 0x24) " " (space, 0x20) "’" (single quote, 0x27) "~" (tilde, 0x7e) now can come regex searches chars in set. need regex matches chars out of set can replace them nothing.
any ideas?
here way can it. tagged perl, give perlish solution:
my $string = q{that ~ v%^&*()ery co$ol ' not 4 realistic t3st}; print $string . "\n"; $string =~ s{[^-a-za-z '~]}{}g; print $string . "\n"; prints:
that ~ v%^&*()ery co$ol ' not 4 realistic t3st ~ cool ' not realistic tst to make clear:
$string =~ s{[^-a-za-z '~]}{}g; matches chars not [^..] inside [,] parenthesis , replace them nothing. g flag @ end of substitution replacing more 1 character.
Comments
Post a Comment