PHP Preg_match without the BS
Your guide to becoming a preg_match/regex genius. Or just coding like me
. 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];



March 17th, 2008 at 5:45 pm
I find it easier to start and end my regex’s with # instead of a /
March 18th, 2008 at 5:31 am
To be honest, I’m the sort of person who finds a method that works once and sticks to it
. 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.
March 19th, 2008 at 7:54 pm
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.
March 19th, 2008 at 7:56 pm
oops…
preg_match_all(”#http://www.GOOGLE.com/search\?hl=en&q=(.*?)&btnG=Google+Search#si”, $url, $m);
print_r($m);
March 22nd, 2008 at 11:57 am
i like the word ‘lazt programming’,bacuase i’m a lazy programmer.i’m always looking the easiest way to solve programming problem.^_^
March 25th, 2008 at 3:50 am
nice info, i never know *? as a quantifier in regex