I might approach the problem slightly differently, with an inverted character class:
c:\@Work\Perl\monks>perl -wMstrict -le
"use constant NOT_ALLOWED => qr{ [^-\x27\x23\$\@0-9a-zA-Z.\\/_%(){}&!~
+`^] }xms;
;;
for my $str ('', qw(ABC_ABC 'A' ///?/// =*+| ABC=), ' ', qq{\t
+\r\n}) {
my $status = 0 + $str =~ NOT_ALLOWED;
print qq{[$str] $status};
}
"
[] 0
[ABC_ABC] 0
['A'] 0
[///?///] 1
[=*+|] 1
[ABC=] 1
[ ] 1
[
] 1
Note that:
-
A ^ (caret) in the first position in a character class inverts the contents of the class.
-
A - (hyphen) in the first position in a character class (or in the first position after an initial caret, if there is one), or in the last position in the class is a literal hyphen character. Everywhere else it is a character range operator as in A-Z and 0-9 elsewhere in the example class.
-
The $ @ characters should always be escaped, as otherwise they will often be interpreted as interpolable variables.
-
Finally, \x27 \x23 are how I have to represent the ' # characters, respectively, because my REPL doesn't like these literal characters in qr// and similar operators. (Have to fix that someday.)
(And a lot more test cases wouldn't be a bad idea.)
Give a man a fish: <%-{-{-{-<
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.