Jump to content
Sign in to follow this  
agahi

find any combination in any order of specific characters.

Recommended Posts

Hi everybody

I am trying to figure out a regex that match any combination/order of digits and backslashes for example it should matches all the following strings:

 

$string = "1423/3838/894789/9387";

or 

$string = "/73636/83/398/3";

or 

$string = "3//33/3838/7474/"

or 

$string = "/";

or 

$string = "636"

 

AND NOT:

$string = "a3434";

 

Any idea?

Share this post


Link to post
Share on other sites
'~\\A[\\d/]+\\z~'

By the way, those are forward slashes, not backslashes.

Edited by Jacques1

Share this post


Link to post
Share on other sites

Thanks Jacques, it worked perfectly.

I have a follow up question, lets say I am also certain that there is going to be less than 4 alphabetical characters there and I want to match that as well for example my  string would be:

 

$string = "1423/3838/894789BLAH/9387";

or 

$string = "/73636/83/398/3BLAH";

or 

$string = "BLAH3//33/3838/7474/"

or 

$string = "/BLAH";

or 

$string = "636BLAH"

 

AND NOT because there are more than 4 alphabetical characters:

$string = "BELABELA3434";

 

I modified your code as follow but it didn't work.

'~\\A[\\d/\S{0,3}]+\\z~'

Share this post


Link to post
Share on other sites

So you're allowing up to 4 alphabetical characters in one block? Or can I have something like “1A2B”?

 

For the former:

'~\\A[\\d/]+[a-z]{0,4}[\\d/]+\\z~i'

Read: an arbitrarily long sequence of digits and forward slashes followed by up to 4 alphabetical characters followed by another arbitrarily long sequence of digits and forward slashes.

Share this post


Link to post
Share on other sites

Nonsense, this doesn't cover the last two cases. Let's try it again:

'~\\A(?:[\\d/]+[a-z]{0,4}|[a-z]{1,4})[\\d/]*\\z~i'

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.