in reply to Regular expression to check for invalid file name characters

sub is_valid_name {$_[0] !~ m{[\\/:*?"<>|]}}
Note however that this allows an empty name (which isn't valid in most filesystems). Also note that it's filesystem dependent what are valid and invalid character. For instance, most UNIX filesystems allow any character, except "\x{00}" and "/", the former being a C string terminator convention, the latter being the path separator.

Replies are listed 'Best First'.
Re^2: Regular expression to check for invalid file name characters
by stefbv (Priest) on Feb 22, 2010 at 11:56 UTC

    Maybe a character class of what is allowed would be a better approach, users can be very inventive :)

    sub is_valid_name { $_[0] =~ m{[a-zA-Z0-9_ ]} }

    Update: OOps, still learning ..., a negated character class.

    sub is_valid_name { $_[0] !~ m{[^a-zA-Z0-9_ ]} }
    Update2: JavaFan is right, this is not the right way to do it.

    Update3: ... and I need my salary and don't want to upset the rest of the world ;)

      So, no Unicode or accented characters or even dots or hyphens? Your fellow Chinese, Korean, French, Swedish, Indian and Arabic Perlmonks will not be happy with that. Neither will your fellow Perl or C coders. (Nope, can't have Module.pm. Nor main.c. salary_run_2010-02? No payment for you!)

      Sometimes, it's just easier to list what you want to exclude.