Regular Expression to Validate File Path and Extension

Published on Tue Mar 01 2022

Recently, I was looking for a regular expression to validate a file path and extension. I found several of them on Google, but none of them fit my requirement. So I decided to put together a version that suits my need. Here is the Regular Expression to validate the file path and extension and it is compatible with JavaScript and ASP.NET. I hope someone will find this information useful and that it will make your programming job easier. Hide Copy Code ^(?:[\w]\:|\\)(\\[a-z_\-\s0-9\.]+)+\.(txt|gif|pdf|doc|docx|xls|xlsx)$ Explanation Hide Copy Code ^(?:[\w]\:|\\) -- Begin with x:\ or \\ [a-z_\-\s0-9\.] -- valid characters are a-z| 0-9|-|.|_ (you can add more) (txt|gif|pdf|doc|docx|xls|xlsx) -- Valid extension (you can add more) Matches Hide Copy Code \\192.168.0.1\folder\file.pdf \\192.168.0.1\my folder\folder.2\file.gif c:\my folder\abc abc.docx c:\my-folder\another_folder\abc.v2.docx Non-Matches Hide Copy Code \\192.168.0.1\folder\fi<le.pdf \\192.168.0.1\folder\\file.pdf \\192.168.0.1\my folder\folder.2\.gif c:\my folder\another_folder\.docx c:\my folder\\another_folder\abc.docx c:\my folder\another_folder\ab*c.v2.docx c:\my?folder\another_folder\abc.v2.docx file.xls

Additional matching regexes for
Regular Expression to Validate File Path and Extension

Regular Expression to Validate File Path and Extension

Recently, I was looking for a regular expression to validate a file path and extension. I found several of them on Google, but none of them fit my requirement. So I decided to put together a version that suits my need. Here is the Regular Expression to validate the file path and extension and it is compatible with JavaScript and ASP.NET. I hope someone will find this information useful and that it will make your programming job easier. Hide Copy Code ^(?:[\w]\:|\\)(\\[a-z_\-\s0-9\.]+)+\.(txt|gif|pdf|doc|docx|xls|xlsx)$ Explanation Hide Copy Code ^(?:[\w]\:|\\) -- Begin with x:\ or \\ [a-z_\-\s0-9\.] -- valid characters are a-z| 0-9|-|.|_ (you can add more) (txt|gif|pdf|doc|docx|xls|xlsx) -- Valid extension (you can add more) Matches Hide Copy Code \\192.168.0.1\folder\file.pdf \\192.168.0.1\my folder\folder.2\file.gif c:\my folder\abc abc.docx c:\my-folder\another_folder\abc.v2.docx Non-Matches Hide Copy Code \\192.168.0.1\folder\fi<le.pdf \\192.168.0.1\folder\\file.pdf \\192.168.0.1\my folder\folder.2\.gif c:\my folder\another_folder\.docx c:\my folder\\another_folder\abc.docx c:\my folder\another_folder\ab*c.v2.docx c:\my?folder\another_folder\abc.v2.docx file.xls

Date validation

A regular expression to validate a Date in format DD/MM/YYYY

C++ operator overload

"Lazy" operator overload regular expression: += and likes are left out because + and like matches anyway.

shortcode wordpress

Wordpress shortcode regular expression

Binary code regular expression

Regular expression for binary code.

Youtube channel link regular expression

Regular expression for youtube channels links.

SIP Address Validator

This Regular Expression Validates SIP Addresses

ds100reg4

Data 100 Regular Expression 4, adapted from Princeton

Letters, numbers and blank space

The following regular expression matches only letter a-z A-Z numbers 0-9 and a blank space for a given string.

Regular Expression For Date

Regular expression for asp:RegularExpressionValidator with format MMddyy. i have Resolved leap year issue as well as month ending day issue with Regular expression