in reply to Regex to detect file name

I would instead specify what is allowed, which makes for a simpler regular expression in your case:

if($view_tag =~ m/\A[A-Za-z0-9][A-Za-z0-9_\-\.]+\z/) { # everything is OK } else { die "Invalid/disallowed filename '$view_tag'"; };

Replies are listed 'Best First'.
Re^2: Regex to detect file name
by lirc201 (Initiate) on Jul 05, 2018 at 19:07 UTC
    Thank you so much. I actually just changed the =~ to !~ and it works as expected with your regex.
      I actually just changed the =~ to !~ and it works as expected with your regex.

      I don't understand from the OP what you want, and so I don't understand how the logical negation of Corion's regex gives you what you want. Can you elucidate, perhaps with some matching and non-matching example strings? Are you sure you're really matching what you think you're matching? (Please see perlre, perlretut, and perlrequick.)


      Give a man a fish:  <%-{-{-{-<

        Hm, I think I understand the problem. The OP's regex:
        if($view_tag =~ m/[^A-Za-z0-9_\-\.]/) {
        actually has a negative character class (leading ^), rather than a start-of-string anchor.

        So the OP had to negate the pattern.

        My guess is that the OP really meant:

        if($view_tag =~ m/^[A-Za-z0-9_\-\.]/) {
        Update: fixed a typo: s/or-string/of-string/