Jump to content

regular expression for specific format


Ninjakreborn

Recommended Posts

I am trying to test for
00-00
which is numbernumberdashnumbernumber
just like that, it will also always be liek that.
I am not having much lukc with the expression, so far I have
[00]-[00] THat was my first attempt, then I noticed it failed. so I tried
[0-9]-[0-9] That didn't work either.
So I modified it like this
^[0-9]-[0-9]$ That didn't seem to work, any advice?
I want to just make a regular expression
$reg = "pattern here"
then
if (pregmatch($reg, $variable);
However I can't get it to work, are any of these even close?
Link to comment
https://forums.phpfreaks.com/topic/35271-regular-expression-for-specific-format/
Share on other sites

[code]$reg = "([0-9]{2})-([0-9]{2})";
if (!preg_match($reg, $_POST['pastyears'])) {
echo "Past Years Formatted Incorrectly, must be 00-00 format.<br />";
$temp = "yes";
}[/code]
That returns something like
[quote]Warning: preg_match() [function.preg-match]: Unknown modifier '\' in page(path to file)[/quote]
Does anyone see what I might be doing wrong?  It worked when I ran that through a tester.
You can reduce[tt] [0-9]{2} [/tt]to[tt] \d{2}[/tt]. The use of[tt] $ [/tt]is good, but subtly wrong in this case (which I'm often just as guilty of). Use[tt] \z [/tt]instead, because[tt] $ [/tt][i]can[/i], but not necessarily, match before a new line.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.