FireworksColdFusionDreamweaverFreehandFlashMXHome
Latest New Content

Latest Free Content
View All
Free Content
Accessibility
CMX Learning Guides
Hosted by enterhost

Introduction to Regular Expressions

By: Rob Williams

Page 1 of 3

Set for printing

Next

Strings, to most programmers and web developers, are horrific and tedious things. After all, for a "numbers" geek they don't make sense because you can't use mathematical operators on them, and often times even trying to do simple extractions or replacements can be quite difficult and time consuming.

For the majority of developers they're just a plain old pain to work with, as there never seem to be any "easy" ways to do the things that you'd like. Regardless of which group you belong to, you'll find that things get even worse when you start to work on a project and discover just how frequently some kind of string manipulation is required in order to make everything work the way you want it to!

Fortunately most modern programming/scripting languages include a toolset to help make your "dealings" with strings far easier and more efficient: regular expressions, sometimes called Regex.

Regular expressions?

To be quite honest with you, I really have no idea how the term "regular expression" became associated with the programming tools that bear the name. Frankly there is nothing "regular" about any of the expressions that I've ever used in my scripts, and most of the time those are the very simple ones... forgive me though, I'm getting ahead of myself a bit here.

Regular expressions refer to a standardized set of characters that can be combined to form patterns. The patterns (the "expressions") can then be compared to a string in various ways to either alter it or retrieve specific parts of it.

For example we could retrieve the word "car" from the following string by using a very simple regular expression:

//Define a string
$example = "The car was creeping very slowly down the street.";

//Define a pattern to search for (the "regular expression")
$regEx = "/car/";

//Perform a match test
preg_match($regEx, $example, $resultsArr);

var_dump($resultsArr);

Here the regular expression pattern is "/car/". One interesting thing about regular expressions is that although the concepts (and most special characters) are the same between various scripting/programming languages, the way in which you define them can be slightly different. Generally speaking (in PHP and JavaScript at least) a regular expression is defined between slashes; thus the pattern that PHP is actually trying to compare is simply the three characters in sequence: c, a, r.

The way in which PHP handles and implements regular expressions can be a bit confusing at times. In the previous example, I used the function preg_match, which takes three arguments: the pattern that you want to search for ($regEx), the string that you want to search in ($example), and the variable that you want the results to be returned to ($resultsArr). That last one might seem a bit strange, as you'd expect that to be returned from the function call itself rather than passed as an argument, but that's just the way some PHP functions work (if you're really curious see the aside note below).

Why are some of PHP's functions rather unusual in the values they return and how they operate? The reason is that many of PHP's "extended features" (most of which are commonly considered integral parts of PHP these days) are actually built on top of existing Linux/Open Source programs. All of the preg functions, for example, are built on top of Perl's PCRE library, and thus act in the same way that they would in Perl (a much older language). Many of these "library based" functions are designed to be called in such a way that the immediate return value will indicate to you whether the function call succeed or not (rather than the result of the function call itself). It's one of the small oddities that you have to get used to in order to reap the full benefits and flexibility of a language like PHP.

PHP's oddities aside for a moment though, the comparison of "car" to "The car was moving very slowly" causes $resultsArr to contain one entry:

Array (1)
{
   [0] =>
   string(3) "car"
}

At this point things may not seem very exciting; all we've managed to do, after all, is extract the word "car" from a sentence by telling PHP to specifically look for the word "car". In this type of case we could have simply used a character locating function, like say strpos, to achieve the same goal.

Page 1 of 3 1 2 3 Next


download
Download Support Files


Keywords
Regular expression, PCRE, string manipulation, pattern matching, preg_match, preg_match_all