Jump to content

Match Two Types of Strings

Go to solution Solved by requinix,

Recommended Posts

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.


Link to comment
Share on other sites

  • Solution

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 |



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.

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.