LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-15-2005, 12:56 PM   #1
opioid
Member
 
Registered: Jul 2003
Location: Chicago
Distribution: Ubuntu
Posts: 124

Rep: Reputation: 15
php regular expression for mm-dd-yy


Hey folks, I'm learning regular expressions and I have a question for you regarding how to validate server-side dates like MM-DD-YY.

Can someone give me an example of the regular expression I could match my string against? I have read several pages on this but I'm not seeing how they breakdown.

Thank you!
-O
 
Old 03-15-2005, 01:03 PM   #2
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
A regular expression could only match the format, not the ability to make sure they don't put in 02-31-05 or something.

But this should be functional to match the format:

/[0-9]{1,2}\-[0-9]{1,2}\-[0-9][0-9]/
 
Old 03-15-2005, 01:33 PM   #3
opioid
Member
 
Registered: Jul 2003
Location: Chicago
Distribution: Ubuntu
Posts: 124

Original Poster
Rep: Reputation: 15
Actually, I already have this:

^[0-9]{2}-[0-9]{2}-[0-9]{2}$

What's the difference?

Thanks,
O
 
Old 03-15-2005, 02:17 PM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Your expression means it require 2 digits number to validate, with the one
above yours, one digit and 2 digits numbers match

example :
01-01-05 <-- match
1-1-05 <-- does not match
 
Old 03-15-2005, 04:31 PM   #5
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
I am not sure if the \ in front of the - in mine is required or not: the - indicates a range within [], but it's probably not required. So I think this is preferred:
/[0-9]{1,2}-[0-9]{1,2}-[0-9]{2}/
If you want to permit 4 digit years (OPTIONALLY), try this:
/[0-9]{1,2}-[0-9]{1,2}-([0-9]{2})?[0-9]{2}/

(I believe that requires extended regular expression matching for the ?)
 
Old 03-17-2005, 12:36 PM   #6
opioid
Member
 
Registered: Jul 2003
Location: Chicago
Distribution: Ubuntu
Posts: 124

Original Poster
Rep: Reputation: 15
Tight! Thanks keefaz and Matir!
 
Old 03-17-2005, 02:34 PM   #7
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
Three things:

1. Don't forget word boundaries. Wrap the regex in \b \b, otherwise, you could match a:
222-02-02
2. Don't forget that \d is the same as [0-9]. This will shorten the regexp, and make it less sloppy (if that can be said about a regexp).
3. Instead of saying "[0-9]{1,2}", you could shorten the regexp by saying "\d?\d". That's half the length for one segment. (=
 
Old 03-17-2005, 03:22 PM   #8
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
I'm not sure all implementations of regexp engines support \b and \d. I may be wrong though (not that uncommon actually). My regular expression is also a bit easier to read (if that can be said for a regexp).
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with regular expression aecaudel Programming 6 11-04-2005 05:28 AM
Need help with Regular Expression subaruwrx Linux - Newbie 6 09-04-2004 07:48 PM
Anyone know regular expression? ahhua Linux - Software 1 12-04-2003 08:13 AM
regular expression gumby Programming 3 07-15-2003 12:13 PM
using a perl regular expression in php markus1982 Programming 5 11-18-2002 02:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration