A regular expression to match valid filenames. It can be used to validate filenames entered by a user of an application, or the filename of files uploaded from a scanner.
The expression ensures that your filename conforms to specific rules, including no leading or trailing spaces and no use of any characters besides the letters A-Z and numbers 0-9.
/^[a-zA-Z0-9](?:[a-zA-Z0-9 ._-]*[a-zA-Z0-9])?\.[a-zA-Z0-9_-]+$/
Matches:
- regex.txt
- 1.2
- patten.exe
Non-matches:
- .htaccess
- &.jpg
- 你好.py
See Also:
- Regex To Check If Are Allowed File Types
- Windows File Path Regular Expression
- Linux File Path Regular Expression
- Regex To Extract Filename From A Path