> > Yes all non-alphanumeric and non-whitespace. I have a > database for people to search using a keyword or all > alphanumerical words (including space) are valid, but no > special characters. All the characters I am substituting > below are invalid characters for the search. >
If you are happy whith alphanumeric, whitespace _and_ underscore "_" you can use "\w" => Match a "word" character (alphanumeric plus "_") If not, use "[:alnum:]" As I tend to be userfriendly I would tell the user what he did wrong, therefore the while loop ------------------------------------- underscore allowed: ------------------------------------- #!/usr/bin/perl use strict; use warnings; my @phrases = ("Audio A4 Quattro", "tom & % jerry", "bla *\nblubb", "underscore _") ; foreach my $phrase (@phrases) { while ($phrase =~ /[^\s\w]/g) { print "Found \"$&\" in $phrase. Allowed characters are 0...1, a..z, A..Z, _ and whitespace. \n"; } } --------------------------------------- underscore not allowed --------------------------------------- #!/usr/bin/perl use strict; use warnings; my @phrases = ("Audio A4 Quattro", "tom & jerry", "bla *\nblubb", "underscore _") ; foreach my $phrase (@phrases) { while ($phrase =~ /[^\s[:alnum:]]/g) { print "Found \"$&\" in $phrase. Allowed characters are 0...1, a..z, A..Z and whitespace. \n"; } } --------------------------------------- your code would be changed to (underscore allowed): while ($keyword =~ /[^\s\w]/g) { print "Found \"$&\" in $phrase. Allowed characters are 0...1, a..z, A..Z, _ and whitespace. Please try again\n"; } cheers, gabi -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/