...

View Full Version : Stupid preg_match pattern



fl00d
08-08-2008, 06:51 AM
I'm trying to check if a date matches a pattern (DD/MM/YY) using preg_match and I can't figure out the damn proper pattern. I've read all over about modifiers and the delimiters and I still can't get it to work.

My pattern so far...


$datePattern = "^[0-9]{2}\/[0-9]{2}\/([0-9]{2}$"; //Format DD/MM/YY


Seems like it should be extremely simple but I could never wrap my head around regular expressions, thus why I try to avoid them as much as possible. I thought {2} was supposed to determine how long the string is allowed to be but according to PCRE manual it means
{n} Match exactly n times.
Frustrating :/

Can someone help me out?

Edit-
I tried some grouping w/() but still couldn't get it to work. Grr... I have no idea

oesxyl
08-08-2008, 07:17 AM
I'm trying to check if a date matches a pattern (DD/MM/YY) using preg_match and I can't figure out the damn proper pattern. I've read all over about modifiers and the delimiters and I still can't get it to work.

My pattern so far...


$datePattern = "^[0-9]{2}\/[0-9]{2}\/([0-9]{2}$"; //Format DD/MM/YY


Seems like it should be extremely simple but I could never wrap my head around regular expressions, thus why I try to avoid them as much as possible. I thought {2} was supposed to determine how long the string is allowed to be but according to PCRE manual it means
{n} Match exactly n times.
Frustrating :/

Can someone help me out?

Edit-
I tried some grouping w/() but still couldn't get it to work. Grr... I have no idea



$dateval = "08/12/2008"; // work with this format
$dateval = "08/12/08"; // or this
preg_match("/^(\d{2})\/(\d{2})\/(\d{2,4})$/",$dateval,$res);
print_r($res);


regards

fl00d
08-08-2008, 07:35 AM
Thank you very much oesxyl! I don't understand why I don't understand regular expression implementation... it's a bit weird.

oesxyl
08-08-2008, 07:57 AM
Thank you very much oesxyl! I don't understand why I don't understand regular expression implementation... it's a bit weird.
:), is same thing like math, nothing to understand just apply the given rules.
1. use the classes from the kind of engine you use, keep in mind differences and common thing
2. take from left to right, don't think but the current position
3. build step by step and test

I lie if I say that I understand something, :)

regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum