Decrease Font Size
Increase Font Size
   BLOG

Regular Expression to validate file path and extension

by bryian 24. June 2011 18:22

Regex that matches file path and extension |

Regular expression for full file path and file extension |

Using Regular Expressions to validate a filename and path|

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 compatible with JavaScript and ASP.NET.

^(?:[\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:
http://regexpal.com/
http://regexlib.com/



Regular expression to extract inner text from anchor tags

by bryian 8. February 2011 18:24

Regular expression to extract inner text from anchor tags |

Regular Expressions Extract Anchor Text |

How to extract inner text of the hyperlink using Regular Expression in C#|

Regular Expression to remove anchor tags

 

Several days ago, someone at the forum has asked how to extract the text from a hyperlink and preserve other HTML tags. It sound interesting, I did some research but can't find the direct solution. So, I decide to put together a simple regular expression to execute the task.

Regular Expression: (<[a|A][^>]*>|</[a|A]>)

Explanation:

<[a|A][^>]*> -- Remove <a href="a.aspx">
</[a|A]> -- Remove </a> tag

Example 1:

string str1 = "<a href=\"http://www.amazon.com/dp/0596528124/\" class=\"someclass\">Mastering Regular Expressions</a> 
-- <A href=\"http://cnn.com/\">CNN</a> <div><a href=\"http://blog.ysatech.com\">http://blog.ysatech.com</a></div>";

str1 = System.Text.RegularExpressions.Regex.Replace(str1, "(<[a|A][^>]*>|</[a|A]>)", "");

Result: Mastering Regular Expressions -- CNN <div> http://blog.ysatech.com </div>

Example 2:

string str2 = "<div><a href=\"http://www.ysatech.com/\" class=\"someclass\">ysatech</a></div>";

str2 = System.Text.RegularExpressions.Regex.Replace(str2, "(<[a|A][^>]*>|</[a|A]>)", "");

Result: <div>ysatech</div>

Test this regular expression here.