The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Regex Programming
|
Other - Regex to validate file extension is pdf or no file extension (a Mac File)
Discuss Regex to validate file extension is pdf or no file extension (a Mac File) in the Regex Programming forum on Dev Shed. Regex to validate file extension is pdf or no file extension (a Mac File) Regular expressions forum covering PCRE and POSIX techniques, practices, and standards. Regular expressions help shorten coding time by providing the ability to compact many lines of code into one string.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

June 13th, 2012, 08:08 AM
|
|
Registered User
|
|
Join Date: Jun 2012
Posts: 4
Time spent in forums: 44 m 2 sec
Reputation Power: 0
|
|
|
Other - 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?
|

June 13th, 2012, 12:29 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
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.
|

June 13th, 2012, 12:46 PM
|
|
Registered User
|
|
Join Date: Jun 2012
Posts: 4
Time spent in forums: 44 m 2 sec
Reputation 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.
|

June 14th, 2012, 01:06 AM
|
 |
Contributing User
|
|
Join Date: Apr 2012
Location: spaceBAR Central
|
|
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
}
|

June 14th, 2012, 07:34 AM
|
|
Registered User
|
|
Join Date: Jun 2012
Posts: 4
Time spent in forums: 44 m 2 sec
Reputation Power: 0
|
|
|
Working in C#/asp.net. Didn't work. Thanks, though.
|

June 18th, 2012, 07:08 PM
|
 |
Contributing User
|
|
Join Date: Apr 2012
Location: spaceBAR Central
|
|
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
}
|

June 19th, 2012, 10:08 AM
|
|
Registered User
|
|
Join Date: Jun 2012
Posts: 4
Time spent in forums: 44 m 2 sec
Reputation 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.
|

June 19th, 2012, 08:17 PM
|
 |
Contributing User
|
|
Join Date: Apr 2012
Location: spaceBAR Central
|
|
|
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/
|

June 19th, 2012, 09:32 PM
|
 |
Contributing User
|
|
Join Date: Apr 2012
Location: spaceBAR Central
|
|
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|