j.smith1981 Posted January 4, 2012 Share Posted January 4, 2012 Hi there, I was wondering if I could pick someones brains please? I am doing some work in regular expressions again, I thought why not have some fun with it, well I have setup this literal example but a bit baffled with what I am trying to do. Here's my logic: <?php preg_match('/index.php\?cat=\d\d&post=\d\d/', 'index.php?cat=25&post=65', $matches); What I would like to do is allow the \d\d (2 digits) to be anything like say i don't know 1 10 even 100000 you see? Is this possible if so how? I am trying to think about this critically but failing to do so to add to this, just though it would be fun any helps much appreciated, Jeremy. Quote Link to comment https://forums.phpfreaks.com/topic/254350-having-fun-with-regular-expressions-but-want-a-more-dynamic/ Share on other sites More sharing options...
Adam Posted January 4, 2012 Share Posted January 4, 2012 If you have two digits directly next to each other, you just need to use one \d because it will match both at once. Quote Link to comment https://forums.phpfreaks.com/topic/254350-having-fun-with-regular-expressions-but-want-a-more-dynamic/#findComment-1304198 Share on other sites More sharing options...
j.smith1981 Posted January 4, 2012 Author Share Posted January 4, 2012 Tried this: <?php preg_match('/index.php\?cat=\d&post=\d\d/', 'index.php?cat=25&post=65', $matches); Does not now come up with any results, there must be a method for saying up to the next character which would be a letter, but I just can't work out the logic. Quote Link to comment https://forums.phpfreaks.com/topic/254350-having-fun-with-regular-expressions-but-want-a-more-dynamic/#findComment-1304201 Share on other sites More sharing options...
Adam Posted January 4, 2012 Share Posted January 4, 2012 Ah.. You're missing a plus after \d, to mean 'match once or more'. That will effectively then match anything up to the next character which isn't a number. Quote Link to comment https://forums.phpfreaks.com/topic/254350-having-fun-with-regular-expressions-but-want-a-more-dynamic/#findComment-1304206 Share on other sites More sharing options...
j.smith1981 Posted January 4, 2012 Author Share Posted January 4, 2012 Finally my heads kicked in lol: <?php preg_match('/index.php\?cat=\d{1,}&post=\d{1,}/', 'index.php?cat=25&post=65', $matches); {1,} means the character before the iteration must appear at least once but multiple times, anyways it works, tried even with {1,5} and deliberately entered 6 characters in the numeric part and it still works even, well not results come up but if I was to put in 6 using the {1,} then it would work. Thanks for your help, Jeremy. Quote Link to comment https://forums.phpfreaks.com/topic/254350-having-fun-with-regular-expressions-but-want-a-more-dynamic/#findComment-1304207 Share on other sites More sharing options...
Adam Posted January 4, 2012 Share Posted January 4, 2012 Simpler syntax: \d+ Quote Link to comment https://forums.phpfreaks.com/topic/254350-having-fun-with-regular-expressions-but-want-a-more-dynamic/#findComment-1304208 Share on other sites More sharing options...
j.smith1981 Posted January 4, 2012 Author Share Posted January 4, 2012 Sorry the last post I made does exactly the same thing I think really must try and get my brain working though but thank you so much is there any real difference though between the 2? I suppose with the {} curly braces you could have a min set and the plus (+) would just say 1 or more (which I remember being the metachar for. Am I thinking in the right sense between + and {1,} ? Finally understanding some rather simple but dynamic regexes really happy with what I am doing. Really appreciate your help though! Jeremy. Quote Link to comment https://forums.phpfreaks.com/topic/254350-having-fun-with-regular-expressions-but-want-a-more-dynamic/#findComment-1304209 Share on other sites More sharing options...
Adam Posted January 4, 2012 Share Posted January 4, 2012 + and {1,} mean exactly the same thing. * is also an alias of {0,} which means zero or more times. Your regular expression (no offence) is very simple, but when you move onto longer and more complex patterns they are a subtly large benefit. There's no need to use curly brackets unless you need to specify two or more times, three or more times, etc. Quote Link to comment https://forums.phpfreaks.com/topic/254350-having-fun-with-regular-expressions-but-want-a-more-dynamic/#findComment-1304217 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.