• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 961
  • Last Modified:

Regex to validate date/partial date for sql

hi can someone please give me a regex which will help me validate dates and which works in php4. it needs to allow numbers, hyphens and percentage signs (it will be used in a sql query)

some examples of valid strings:

2007-08-27
2007-08%
2007%
%-08-%
%-15

thankyou!

(trying to understand a bit of regex here; but whatever I try just isn't doing what it seems it should!)
0
majiga
Asked:
majiga
  • 5
  • 4
2 Solutions
 
DarkoLordCommented:
^[\d-%]+$
0
 
DarkoLordCommented:
"/^[\d-%]+$/" would be the entire string you pass to the PHP regex method.

To explain:
^ - start of string
[\d-%] - matches any of the characters in [ ], which is digit (\d), hyphen or percentage sign
+ - repeat one or more
$ - end of string
0
 
majigaAuthor Commented:
thanks for that but preg_match says: "No ending delimiter '^' found" and egregi says "REG_ERANGE".

I tried removing the % but it still returns false

^([0-9-]+)$   works with eregi (it doesnt like the %)

I'm considering just going with that and slapping a % on either side of it once the regex has been found true... thoughts?

thanks
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
DarkoLordCommented:
preg_match works if you put a slash / at the each end of the regex:

preg_match("/^[\d-%]+$/", "2007-08%", $matches);
0
 
ebosscherCommented:
you'll probably have to escape the odd looking characters in the regex?

/^[\d\-\%]+$/

although that regex won't work the way you want that would actually match the following

2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2

so, i would probably stay away from it  I'll see what i can come up with.  You date format will always be yyyy-MM-dd?
0
 
ebosscherCommented:
the following script will show that this regex matches all the items you listed above:
<?php
$pattern = '/(?:\d{4}|%)-?(?:\d{2}|%)?-?(?:\d{2}|%)/';

echo '2007-08-27: ' . (preg_match($pattern, '2007-08-27') == 1 ? 'matched' : 'no match') . "\r\n";
echo '2007-08%: ' . (preg_match($pattern, '2007-08%') == 1 ? 'matched' : 'no match') . "\r\n";
echo '2007%: ' . (preg_match($pattern, '2007%') == 1 ? 'matched' : 'no match') . "\r\n";
echo '%-08-%: ' . (preg_match($pattern, '%-08-%') == 1 ? 'matched' : 'no match') . "\r\n";
echo '%-15: ' . (preg_match($pattern, '%-15') == 1 ? 'matched' : 'no match') . "\r\n";
?>
0
 
ebosscherCommented:
oh, in case i forgot to mention it will force the string to be, at a minimum, one of the following:

%%
9999%
%99

yeah, the %% sucks, but I wasn't about to put the lookahead into it, you can if you want, or just handle that one condition with a conditional test after the fact
0
 
DarkoLordCommented:
With ebosccher's regex, also these item match (you probably don't want that):

sadasdsa2322007-08-272423423432
123%-15121212
....

Try this one:

$pattern = '/^(?:[1-2]\d{3}|%)(?:(?:-?%$)|-(?:\d{2}(?:(?:-?%$)|-\d{2})|\d{2}$))/';
0
 
ebosscherCommented:
LOL, nice... add two characters and call it your own I guess.  I can see your point, and I get the retaliation for my comment, but please, at least i put some effort into it.
0
 
ebosscherCommented:
ok, it was more than two characters, my appology...  you've actually enhanced it quite a bit, i take my snarky comment back.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now