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'; if(ereg("$year","$date_of_birth")) { 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:
- www.regexlib.com provides a large library of pre-built regular expressions.
- Here is a simple introduction to regular expressions. The pages are geared towards using regular expressions in Visual Basic for Applications, but the concepts are general in nature and would apply to most other situations as well.
- www.regular-expressions.info provides a fairly technical introduction and lots of advanced detail as well.
- Wikipedia entry, giving a detailed discussion of regular expressions.
Copyright © 2000-2024 Tiger Technologies LLC