About Regular Expressions

Regular expressions are specially-formatted strings which are used to see if another string contains some desired text. (The term regular expression is often abbreviated as regex or regexp.)

For example, the regular expression "[bcf]og" means "match the letter b or c or f, followed by og". This regular expression would match the strings "bog", "cog", and "fog", but it would not match "hog".

Most of the languages that you might use in developing your website offer regular expression support. For instance here is a code snippet of how you could use a regular expression in PHP to check if someone was born in the year 2000:

$year = '2000';
$name = 'John';
$date_of_birth = '2000-01-24';

  echo "$name was born in $year.\n"; // prints "John was born in 2000."

And here is another example in Perl which performs a surname match:

my $surname = 'Smith';
my $name = 'John Smith';

# "$" means the match must have nothing after "Smith"
# "i" make the match case insensitive
if($name =~ /$surname$/i) 
  print "$name is a $surname.\n"; # prints "John Smith is a Smith."

The examples above are fairly simple regular expressions. However, regular expressions can be much more powerful. Here are some useful links which cover them in much more detail:

  • Wikipedia entry, giving a detailed discussion of regular expressions.