I think it's what you think is a character class. For some reason that compiles, but the debug output shows something curious when I isolated the character class.
use strict;
use warnings;
use re 'debug';
my $re = qr/[[a-zA-Z0-9]\-\.]/;
And the debut output (from use re 'debug'
) shows this:
Compiling REx "[[a-zA-Z0-9]\-\.]"
Final program:
1: ANYOF[0-9A-[a-z][] (12)
12: EXACT <-.]> (14)
14: END (0)
anchored "-.]" at 1 (checking anchored) stclass ANYOF[0-9A-[a-z][] minlen 4
So it's looking for the literal string '-.]'
as an "anchor". Thus if your hostname does not have '.-]
' in it will never match. Thus it is like I said before, you're closing your character class with the first non-escaped ']'
.
The best way to include a dash is to make it the last character of the class--so as to remove the possibility that it can indicate a range.
In addition, it should all just be one class. You actually close the class with the first non-escaped square-bracket close. Your character class should read:
[a-zA-Z0-9.-]
And that's all.
In addition, it's probably better practice to use named character classes as well:
[\p{IsAlnum}.-]
- Another interesting thing I found out is that in
']'
is interpreted as a literal square-close wherever a character class is not open. Thus you only need to escape it to avoid ending a character class, and thus, included it. Conversely '[['
will include '['
into the character class, thus there is no reason to escape a '['
unless outside of a character class.