Regular expression for valid filename

后端 未结 10 1630
被撕碎了的回忆
被撕碎了的回忆 2020-12-29 18:11

I already gone through some question in StackOverflow regarding this but nothing helped much in my case.

I want to restrict the user to provide a filename that shoul

10条回答
  •  情深已故
    2020-12-29 19:02

    In case someone else needs to validate filenames (including Windows reserved words and such), here's a full expression: \A(?!(?:COM[0-9]|CON|LPT[0-9]|NUL|PRN|AUX|com[0-9]|con|lpt[0-9]|nul|prn|aux)|[\s\.])[^\\\/:*"?<>|]{1,254}\z

    Extended expression (don't allow filenames starting with 2 dots, don't allow filenames ending in dots or whitespace):

    \A(?!(?:COM[0-9]|CON|LPT[0-9]|NUL|PRN|AUX|com[0-9]|con|lpt[0-9]|nul|prn|aux)|\s|[\.]{2,})[^\\\/:*"?<>|]{1,254}(?

    Edit: For the interested, here's a link to Windows file naming conventions: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx

提交回复
热议问题