agahi Posted September 9, 2016 Share Posted September 9, 2016 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? Quote Link to comment https://forums.phpfreaks.com/topic/302117-find-any-combination-in-any-order-of-specific-characters/ Share on other sites More sharing options...
Jacques1 Posted September 9, 2016 Share Posted September 9, 2016 (edited) '~\\A[\\d/]+\\z~' By the way, those are forward slashes, not backslashes. Edited September 9, 2016 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/302117-find-any-combination-in-any-order-of-specific-characters/#findComment-1537215 Share on other sites More sharing options...
agahi Posted September 11, 2016 Author Share Posted September 11, 2016 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~' Quote Link to comment https://forums.phpfreaks.com/topic/302117-find-any-combination-in-any-order-of-specific-characters/#findComment-1537362 Share on other sites More sharing options...
Jacques1 Posted September 11, 2016 Share Posted September 11, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/302117-find-any-combination-in-any-order-of-specific-characters/#findComment-1537376 Share on other sites More sharing options...
Jacques1 Posted September 12, 2016 Share Posted September 12, 2016 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' Quote Link to comment https://forums.phpfreaks.com/topic/302117-find-any-combination-in-any-order-of-specific-characters/#findComment-1537405 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.