Regex Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreRegex Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 13th, 2012, 08:08 AM
jcooperhurt jcooperhurt is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 4 jcooperhurt User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #2  
Old June 13th, 2012, 12:29 PM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,680 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 1 h 55 m 14 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
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.

Reply With Quote
  #3  
Old June 13th, 2012, 12:46 PM
jcooperhurt jcooperhurt is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 4 jcooperhurt User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old June 14th, 2012, 01:06 AM
spacebar208's Avatar
spacebar208 spacebar208 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: spaceBAR Central
Posts: 188 spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 9 h 5 m
Reputation Power: 41
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
}

Reply With Quote
  #5  
Old June 14th, 2012, 07:34 AM
jcooperhurt jcooperhurt is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 4 jcooperhurt User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 44 m 2 sec
Reputation Power: 0
Working in C#/asp.net. Didn't work. Thanks, though.

Reply With Quote
  #6  
Old June 18th, 2012, 07:08 PM
spacebar208's Avatar
spacebar208 spacebar208 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: spaceBAR Central
Posts: 188 spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 9 h 5 m
Reputation Power: 41
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
}

Reply With Quote
  #7  
Old June 19th, 2012, 10:08 AM
jcooperhurt jcooperhurt is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 4 jcooperhurt User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #8  
Old June 19th, 2012, 08:17 PM
spacebar208's Avatar
spacebar208 spacebar208 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: spaceBAR Central
Posts: 188 spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 9 h 5 m
Reputation Power: 41
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/

Reply With Quote
  #9  
Old June 19th, 2012, 09:32 PM
spacebar208's Avatar
spacebar208 spacebar208 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Location: spaceBAR Central
Posts: 188 spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level)spacebar208 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 9 h 5 m
Reputation Power: 41

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > Other - Regex to validate file extension is pdf or no file extension (a Mac File)

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap