LLLLLLL Posted February 27, 2017 Share Posted February 27, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/303304-simple-task-for-you-regexers-out-there/ Share on other sites More sharing options...
requinix Posted February 27, 2017 Share Posted February 27, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/303304-simple-task-for-you-regexers-out-there/#findComment-1543442 Share on other sites More sharing options...
LLLLLLL Posted February 27, 2017 Author Share Posted February 27, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/303304-simple-task-for-you-regexers-out-there/#findComment-1543443 Share on other sites More sharing options...
Solution requinix Posted February 27, 2017 Solution Share Posted February 27, 2017 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}$/' Quote Link to comment https://forums.phpfreaks.com/topic/303304-simple-task-for-you-regexers-out-there/#findComment-1543444 Share on other sites More sharing options...
LLLLLLL Posted February 27, 2017 Author Share Posted February 27, 2017 Right. Quote Link to comment https://forums.phpfreaks.com/topic/303304-simple-task-for-you-regexers-out-there/#findComment-1543446 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.