Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Regular Expression for File Names

Last post 11-06-2009, 9:04 AM by hollyquinn. 3 replies.
Sort Posts: Previous Next
  •  11-05-2009, 11:30 AM 57166

    Regular Expression for File Names

    Hi.  I'm working with an ASP.NET/VB.NET application and I need a regular expression that will validate a TextBox which will contain a File Name.  I need to make it so the user can only enter file names that contain Upper and lower case letters, numbers, hypen, underscore, period, and spaces.  I can't find an example that matches exactly what I'm looking for.  Can anyone help?  Thanks in advance.
  •  11-05-2009, 12:15 PM 57167 in reply to 57166

    Re: Regular Expression for File Names

    I'm sorry I forgot that it would also need to include : \ since it is a file path.
  •  11-05-2009, 5:20 PM 57175 in reply to 57166

    Re: Regular Expression for File Names

    Not tested but what about

    ^[\w .:\\-]*$

    The '\w' will cover the upper and lower case letters, digits and the underscore, and the '\\' will cover the backslash (it needs to be escaped because it is the escape character itself). Also the hyphen character must be the last one in the character set or else it must also be escaped to stop it being interpreted as the range operator.

    I've interpreted "and spaces" as being just the space character (\x20) - if you intend to allow other whitespace characters such as tabs, carriage returns etc , then use the '\s' instead of the ' ' in the character set definition, but this may lead to other issues when the entered text is actually used as a file name.

    Note that this will check the characters but not the structure of the text (i.e. it will allow "asd:::\\\\dsa....dsa-s-d-345")

    Susan

  •  11-06-2009, 9:04 AM 57188 in reply to 57175

    Re: Regular Expression for File Names

    Thank you so much!  That works perfectly.  You are a life saver!

View as RSS news feed in XML