There are good reasons to be a little more selective than that. For instance, allowing things that are not visible ascii characters is generally a bad idea, and including visible ascii characters that typically relate to "shell magic" (vertical bar, ampersand, the various brackets and quotes) can also lead to trouble. More and more, unix-heads are getting used to handling spaces in file names, but allowing TAB, CR and LF as well would be asking for trouble, IMHO.

I'd be more inclined to something like

/\A[^\x00-\x1f\x7f&*?|\\"'`<>{}()[\]]+\z/ # allows space and "safe" punctuation
Of course, that still doesn't address the question of bytes outside the 7-bit ascii range, which includes the whole domain of utf8 wide characters. For that part of the issue, there isn't enough information in the OP to provide a basis for appropriate advice. (Would utf8 file names be allowable/sensible? What is expectable/possible regarding non-ascii content in the input strings?)

In reply to Re^2: How to validate a directory string? by graff
in thread How to validate a directory string? by rapide

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.