Your alternation \$|&|<|>|@|\/ would be better expressed as a character class [$&<>@/].
Not really, no:
use strict; use warnings; my $name = 'whatever'; print $name =~ /[$&<>@/]/ ? 'Yep!' : 'Nope!';
Output:
Unmatched right square bracket at C:\Perl\progs\junk.pl line 5, at end + of line syntax error at C:\Perl\progs\junk.pl line 5, near "/[$&<>@/]" Search pattern not terminated or ternary operator parsed as search pat +tern at junk.pl line 5.
Drat! Let's escape that pesky slash:
my $name = 'whatever'; print $name =~ /[$&<>@\/]/ ? 'Yep!' : 'Nope!';
Output:
Use of uninitialized value in concatenation (.) or string at junk.pl l +ine 5. Nope!
What's happening now? Ah, perl seems to think that $& means $MATCH. Why can't it DWIM? Let's try this:
my $name = 'whatever'; print $name =~ /[&$<>@\/]/ ? 'Yep!' : 'Nope!';
Aah, that's better!
Update: fixed typo, closed blockquote tag.
Update 2: just realised that there's yet another problem. The dollar sign has to be escaped as well:
my $name = 'what$ever'; print $name =~ /[\$&<>@\/]/ ? 'Yep!' : 'Nope!';
In reply to Re^2: Regex and negative file test
by Not_a_Number
in thread Regex and negative file test
by neilwatson
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |