【问题标题】:regex for validating folder name & file name用于验证文件夹名称和文件名的正则表达式
【发布时间】:2011-09-07 12:15:04
【问题描述】:

我想验证文件名

文件或文件夹的名称不应包含 \ / ? % * : | " .

您能否建议我在 preg_match() 中使用的正则表达式?

谢谢。

【问题讨论】:

    标签: regex preg-match validation


    【解决方案1】:

    使用strpbrk() 函数会更高效。

    if (strpbrk($filename, "\\/?%*:|\"<>") === FALSE) {
      /* $filename is legal; doesn't contain illegal character. */
    }
    else {
      /* $filename contains at least one illegal character. */
    }
    

    【讨论】:

      【解决方案2】:

      符合您要求的正则表达式:^[^\\/?%*:|"&lt;&gt;\.]+$

      【讨论】:

        【解决方案3】:
        new RegExp('^[a-zA-Zа-яА-Я0-9_!]+$')
        

        http://regexpal.com/试试这个网站

        【讨论】:

          【解决方案4】:
          new RegExp('^[^\\\\\/\?\%\*\:\|\"<>]+$')
          

          它对我有用。 基本上它需要:

          new RegExp('^[^\\/?%*:|"<>]+$')
          

          【讨论】: