PHP Preg_match without the BS

Your guide to becoming a preg_match/regex genius. Or just coding like me :D . Seriously though when all you want to do is prototype a script and you need to match a string regular expressions seem complicated. To write a proper regular expression there are a huge number of symbols you can use which make the expression more efficient all around.

The only symbols I ever really remember are:

(.*) - Matches anything in the same way as searching for a file by the name *.*
(.*?) - Same as above except it defaults to the fewest number of characters possible.

So if we had the phrase:

I am a pretty crazy guy. I am a pretty crazy guy.

and our regular expression was (note: all regular expression start and end with / unless you know what you’re doing and you have another plan):

/I am a(.*)guy/

our output would be:

 pretty crazy guy. I am a pretty crazy

now if we did this:

/I am a(.*?)guy/

our output is:

 pretty crazy

Welcome to the lazy way of programming ;)

Just to further clarify the full php would be:

preg_match(”/I am a(.*)guy/”, “I am a pretty crazy guy. I am a pretty crazy guy.”, $matches);
echo $matches[1];

6 Responses to “PHP Preg_match without the BS”

  1. Justin Says:

    I find it easier to start and end my regex’s with # instead of a /

  2. Harry Says:

    To be honest, I’m the sort of person who finds a method that works once and sticks to it :D . You obviously know what you’re doing with regex’s and I didn’t really aim this tutorial at people who use these all the time.

    It was supposed to just be a sort of get started tutorial for folks who’ve looked at some of those really complicated expressions and think omg. What does this lot do? I think some of the time you need a dowsing rod just to work out how those things work.

  3. Justin Says:

    Yeah I know what you mean. The reason I use a # instead of a / is because they are less common so you don’t have to escape so many characters making your regex easier to read. Another thing that screwed me up at the beginning was not escaping ?, (, ), and other special characters. Also I use a few modifiers on my regex’s.

    s (. can be white space(\t\n\r))
    i (case insensitive)

    And they go after the second delimiter like
    preg_match_all(”#http://www.GOOGLE.com/search\?hl=en&q=(.*?)&btnG=Google+Search#si”);

    The s isn’t need here since it will only be one line, but hopefully it illustrates the point. Regex’s made me want to throw my computer against the wall more than once, but you will get it eventually.

    Loving the site, keep up the good work.

  4. Justin Says:

    oops…
    preg_match_all(”#http://www.GOOGLE.com/search\?hl=en&q=(.*?)&btnG=Google+Search#si”, $url, $m);

    print_r($m);

  5. skip Says:

    i like the word ‘lazt programming’,bacuase i’m a lazy programmer.i’m always looking the easiest way to solve programming problem.^_^

  6. ngkong Says:

    nice info, i never know *? as a quantifier in regex

Leave a Reply

Enter this code