#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    4
    Rep Power
    0

    Regex to validate file extension is pdf or no file extension (a Mac File)


    The following regex works for forcing all files to be pdf, but breaks/doesn't work when a Mac user uploads a pdf file with no file extension: ^.+\.((?:[pP][dD][fF]))$

    Any suggestions for a regex that works for either a PDF file extension or no file extension?
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,071
    Rep Power
    9398
    Given a name like "document" there is no possible way to tell if it's a PDF or not. Really. Think about it for a second.
    Now if you can look at the file contents, maybe with some MIME-magic tool, then you've got yourself a new option.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    4
    Rep Power
    0
    True that. Actually checking the mime type server side doing exactly that - that's a little magic. What I'm looking at is checking on the client side and just focusing on the extension. Know it has its weaknesses, but I think an extension check will be helpful for a majority of the documents that are uploaded to the site. For the others, I will relie on the server side verfication.
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    229
    Rep Power
    42
    How are you doing the test?

    Code:
    php?
    if (preg_match('/^.+\.((?:[pP][dD][fF]))$/', $subject)) {
    	# Successful match
    } else {
    	# Match attempt failed
    }


    Code:
    perl?
    if ($subject =~ m/^.+\.((?:[pP][dD][fF]))$/) {
    	# Successful match
    } else {
    	# Match attempt failed
    }
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    4
    Rep Power
    0
    Working in C#/asp.net. Didn't work. Thanks, though.
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    229
    Rep Power
    42
    Ok, Then try this:
    Code:
    try {
    	if (Regex.IsMatch(subjectString, @"^.+\.((?:[pP][dD][fF]))$")) {
    		// Successful match
    	} else {
    		// Match attempt failed
    	} 
    } catch (ArgumentException ex) {
    	// Syntax error in the regular expression
    }
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    4
    Rep Power
    0
    Works for selecting against the following file names:

    *.doc
    *.pdf

    Does not work for a file with no extension: example -
    e.g. 2012DetailedSessionOutline

    Using ASP.NET regular expression validation control.

    Thanks for the help.
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    229
    Rep Power
    42
    I'm not sure what you mean when you say it doesn't work, this matches if the file name ends in ".pdf" so any other file name will hit the else(i.e. no match) condition, also check out:
    http://regexpal.com/
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    229
    Rep Power
    42

IMN logo majestic logo threadwatch logo seochat tools logo