user3669539
user3669539

Reputation: 139

Perl regex for 8 digit hexadecimal number (may be missing escape characters)

I have the following code that looks for a folder whose name is a 8 digit hexadecimal number

use strict;
use warnings;

opendir(DIR, ".\\");
my @files = readdir(DIR);
closedir DIR;
foreach my $key (@files)
{
    if(-d ".\\$key")
    {
        if ($key =~ /(^[0-9A-F]{8}?$)/){
            print "$1\n";
        }
    }
}

i tested the regex on regex101 and it is finding the correct strings (8 digit hex) but the code can't find any of the folders i have that have 8 digit hex as name. I suspect it has something to do with certain characters needing to be escaped e.g. $ but i tried many different combinations without success.

e.g. a folder's name i have is 906f3387

Upvotes: 1

Views: 1725

Answers (1)

Les
Les

Reputation: 10605

Your regex is using upper case A-F, but your folder has lower case. Use /^[0-9a-fA-F]{8}$/ to explicitly allow lower case, or /^[0-9A-F]{8}$/i to ignore case in the match.

Upvotes: 8

Related Questions