Click here to Skip to main content
15,881,938 members
Articles / General Programming / Regular Expressions
Tip/Trick

Regular Expression to Validate File Path and Extension

Rate me:
Please Sign up or sign in to vote.
4.95/5 (10 votes)
28 May 2012CPOL 271.1K   14   31
Regular Expression to validate the file path and extension and it is compatible with JavaScript and ASP.NET

Introduction

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.

^(?:[\w]\:|\\)(\\[a-z_\-\s0-9\.]+)+\.(txt|gif|pdf|doc|docx|xls|xlsx)$

Explanation

^(?:[\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

\\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

\\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

Test It

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
I have over 10 years of experience working with Microsoft technologies. I have earned my Microsoft Certified Technology Specialist (MCTS) certification. I'm a highly motivated self-starter with an aptitude for learning new skills quickly.

Comments and Discussions

 
QuestionBlocking Command Injection Pin
ChizI5-Apr-18 2:23
ChizI5-Apr-18 2:23 
AnswerRe: Blocking Command Injection Pin
ChizI5-Apr-18 2:46
ChizI5-Apr-18 2:46 
SuggestionSuggested expression for those who want any or even no extension. Pin
christine.james4-Jan-18 6:24
christine.james4-Jan-18 6:24 
QuestionDoes not work for me - Request Pin
JaiminiThakkar2-Jun-17 8:01
JaiminiThakkar2-Jun-17 8:01 
AnswerRe: Does not work for me - Request Pin
Bryian Tan2-Jun-17 8:44
professionalBryian Tan2-Jun-17 8:44 
GeneralRe: Does not work for me - Request Pin
JaiminiThakkar2-Jun-17 9:21
JaiminiThakkar2-Jun-17 9:21 
GeneralRe: Does not work for me - Request Pin
JaiminiThakkar2-Jun-17 9:22
JaiminiThakkar2-Jun-17 9:22 
GeneralRe: Does not work for me - Request Pin
Bryian Tan2-Jun-17 9:40
professionalBryian Tan2-Jun-17 9:40 
GeneralRe: Does not work for me - Request Pin
JaiminiThakkar2-Jun-17 11:06
JaiminiThakkar2-Jun-17 11:06 
GeneralRe: Does not work for me - Request Pin
JaiminiThakkar2-Jun-17 11:07
JaiminiThakkar2-Jun-17 11:07 
GeneralRe: Does not work for me - Request Pin
Bryian Tan2-Jun-17 11:12
professionalBryian Tan2-Jun-17 11:12 
GeneralRe: Does not work for me - Request Pin
JaiminiThakkar2-Jun-17 11:15
JaiminiThakkar2-Jun-17 11:15 
GeneralRe: Does not work for me - Request Pin
Bryian Tan2-Jun-17 11:22
professionalBryian Tan2-Jun-17 11:22 
GeneralRe: Does not work for me - Request Pin
JaiminiThakkar2-Jun-17 11:26
JaiminiThakkar2-Jun-17 11:26 
GeneralRe: Does not work for me - Request Pin
Bryian Tan2-Jun-17 11:42
professionalBryian Tan2-Jun-17 11:42 
Questionunicode not supported Pin
Member 1251722311-May-16 3:46
Member 1251722311-May-16 3:46 
PraiseWorked for me Pin
Member 773674116-Feb-16 12:07
Member 773674116-Feb-16 12:07 
QuestionThanks Pin
G. Landry8-Jan-16 2:04
G. Landry8-Jan-16 2:04 
SuggestionFile extensions order Pin
Sergei Vedischev19-Jun-14 6:06
Sergei Vedischev19-Jun-14 6:06 
GeneralRe: File extensions order Pin
Bryian Tan30-Jul-14 4:36
professionalBryian Tan30-Jul-14 4:36 
QuestionMore complete version Pin
DeepBlackHole11-Mar-13 17:45
DeepBlackHole11-Mar-13 17:45 
GeneralRe: More complete version Pin
Bryian Tan12-Mar-13 12:57
professionalBryian Tan12-Mar-13 12:57 
QuestionHi Pin
Rogério F Moreira10-Aug-12 5:10
Rogério F Moreira10-Aug-12 5:10 
AnswerRe: Hi Pin
Bryian Tan10-Aug-12 16:53
professionalBryian Tan10-Aug-12 16:53 
QuestionMac and Linux? Pin
Menelaos Vergis28-May-12 23:15
Menelaos Vergis28-May-12 23:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.