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
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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.