PHPBuilder - Advanced String Processing - How Regular Are Your Expressions

RSS Twitter

Advanced String Processing - How Regular Are Your Expressions

by: PHP Builder Staff
May 19, 2009

Hello hello hello, and welcome back. We've looked at strings, and numbers and all sorts of types of data, but we've not yet seen how to do something really important, and that's to look for and pull interesting parts out of the data we have, to do that where going to use some magic from the Perl world called "Regular Expressions"
Huh, Regular What?
Put simply, a regular expression is a string in it's own right, but one that has a special meaning. In some ways it's like a little mini program that tells the regexp engine what to look for and how to find it.
Look at the first line of my opening paragraph above.
If you wanted to look for that "Hello hello hello" and treat it as a spelling error to correct how are you going to find it?
Well you could use

if($text == "Hello hello hello")

or you might use the text function "str_replace":

str_replace("Hello hello hello","Hello");

and they would work fine, but what if we now made the phrase "Hello hello hullo"? hmmm, it looks like we now don't have a match. This is where the power of regular expressions comes to the rescue.
How do Regular Expressions Work Then?
Ok, so your asking yourself, how can I match something that's not match-able unless I change what I'm looking for, which is not what I want to do.
The key is not to change what your searching for but, to just search for the differences.
It's a set of searching rules, that allows for variations in the text to be searched.
What exactly is the difference between "Hello hello hello" and "Hello hello hullo" , well in this case it's only one letter, and that letter can be either an 'e' or a 'u', if we had a way of just saying search for this phrase, but at the 4th letter from the end, you need to be aware that that could change, then you've pretty much defined what a regular expression is.
Now in our example here, we could actually expand that very easily, to be aware of any letter at that position and not just a 'e' or 'u', we do this by using the full stop operator '.' , so to show you what I mean we could write our search pattern like this:

"Hello hello h.llo"

That will match any character at that position, but only one character, all the rest have to match.
There is however much more to the power of reg-expressions than just single letters, we can search for whole groups of numbers, letters and combinations of words symbols. Also for certain counts and lengths.
There's far more than we can cover in this article, developing a true mastery of reg- expressions takes years. We only have time to cover what you need to know to get you started in PHP.
Once snippet of advice I will give, find a program that will allow you to see what your doing as you construct regular expressions. I use the wonderful reg-ex coach available from coach/ There is an older version for linux, but the latest versions are only maintained under Windows now, it does however run perfectly fine under wine.

Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.




(Maximum characters: 1200). You have characters left.