Jump to content
Sign in to follow this  
LLLLLLL

Simple task for you REGEXers out there

Recommended Posts

I suck at Regex. Can someone tell me the PHP regex check to see if a string is 12345-6789 .... meaning, five digits, a dash, and then four digits? I'm sure it's quite simple for a Regex pro.

 

Thank you.

Share this post


Link to post
Share on other sites

You're right, it is quite simple. So what have you tried and what was the problem you had with it? I bet what you came up with was really close.

Share this post


Link to post
Share on other sites

preg_match( '/\d{5}-\d{4}/', $input )

 

My issue was that the - in my original syntax was somehow a ndash... so it was the wrong character. Whoa. Sorry I created this thread.

Share this post


Link to post
Share on other sites

Note that what you have will check if there's that 5-4 somewhere inside $input. As in it will quite happily match with

$input = "a bunch of stuff then 12345-6789 and then more stuff";
If you want to make sure $input has only that 5-4 then you need ^ and $ anchors to mark the beginning and end of the string.

'/^\d{5}-\d{4}$/'

Share this post


Link to post
Share on other sites

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.

Sign in to follow this  

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