in reply to Regex to detect file name

It's probably beating a dead horse at this point, but this looks like what you're trying to do, with explainations:

# original if($view_tag =~ m/[^A-Za-z0-9_\-\.]/) { # corrected if($view_tag =~ m/^[A-Za-z0-9][\w.-]*/) { # to anchor the match at the beginning, # the ^ comes immediately after the first /: m/^[A-Z ... # match the first character [A-Za-z0-9] # matching subsequent characters: [\w.-]* # \w is the same as [A-Za-z0-9_] # . does not need to be escaped when inside brackets # put - last inside brackets, otherwise it may indicate a range # the quantifier * matches 0 or more instances of the characters

Replies are listed 'Best First'.
Re^2: Regex to detect file name
by Laurent_R (Canon) on Jul 06, 2018 at 21:46 UTC
    # corrected if($view_tag =~ m/^[A-Za-z0-9][\w.-]*/) {
    I think that you need to add an end-of-string anchor at the end of the pattern to prevent matching if there are some unwanted or spurious characters after the last character matching [\w.-].