DanRz Posted May 12, 2023 Share Posted May 12, 2023 Hi All, I am terrible at regex. I have the following code, which works fine for number such as 123456M... if(!preg_match('/[1-9]\d{5}M/', $explode[1])) { throw new RuntimeException('Error: Membership number invalid. Please try again.'); } However, I also need to accept number such as TMP1 and TMP1000 - essentially TMP followed by up to 5 numbers... How would I add this too... I need it to error if it doesn't match any of the two types. Thanks Quote Link to comment Share on other sites More sharing options...
Solution requinix Posted May 12, 2023 Solution Share Posted May 12, 2023 So you want to allow both (a) "TMP" plus 1-5 numbers, and (b) 1-5 numbers then "M"? 1. You need ^ and $ anchors, otherwise the regex will only check if the string contains something that matches it. 2. {5} means exactly 5, but you've been saying "up to". 3. What about zeroes? That's not in the regex now but I'd be surprised if you didn't want to allow them. 4. To allow both patterns, tell the regex that you want to allow both patterns using a | /^(\d{1,5}M|TMP\d{1,5})$/ Quote Link to comment 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.