Jump to content

Having fun with regular expressions but want a more dynamic


j.smith1981

Recommended Posts

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.

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

+ 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.

 

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.

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.

×
×
  • 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.