简体   繁体   中英

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 eg $ but i tried many different combinations without success.

eg a folder's name i have is 906f3387

Your regex is using upper case AF, 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM