WebReference.com - PHP and Regular Expressions 101 (3/5)
[previous] [next] |
PHP and Regular Expressions 101
The regular expression syntax
Before you can match a string to a regular expression, you have to create the regular expression. The syntax of regular expressions is a little quirky at first, and each phrase in the expression represents some sort of search criteria. Here's a list of some of the most common regular expressions, as well as an example of how to use each one:
Beginning of string:
To search from the beginning of a string, use ^
. For example,
<?php echo ereg("^hello", "hello world!"); ?>
Would return true, however
<?php echo ereg("^hello", "i say hello world"); ?>
would return false, because hello
wasn't at the beginning of the string.
End of string:
To search at the end of a string, use $
. For example,
<?php echo ereg("bye$", "goodbye"); ?>
Would return true, however
<?php echo ereg("bye$", "goodbye my friend"); ?>
would return false, because bye
wasn't at the very end of the string.
Any single character:
To search for any character, use the dot. For example,
<?php echo ereg(".", "cat"); ?>
would return true, however
<?php echo ereg(".", ""); ?>
would return false, because our search string contains no characters. You can optionally tell the
regular expression engine how many single characters it should match using curly braces. If I wanted
a match on five characters only, then I would use ereg
like this:
<?php echo ereg(".{5}$", "12345"); ?>
The code above tells the regular expression engine to return true if and only if at least five successive characters appear at the end of the string. We can also limit the number of characters that can appear in successive order:
<?php echo ereg("a{1,3}$", "aaa"); ?>
In the example above, we have told the regular expression engine that in order for our search string to match the expression, it should have between one and three 'a' characters at the end.
<?php echo ereg("a{1,3}$", "aaab"); ?>
The example above wouldn't return true, because there are three 'a' characters in the search
string, however they are not at the end of the string. If we took the end-of-string match $
out of the regular expression, then the string would match.
We can also tell the regular expression engine to match at least a certain amount of characters in a row, and more if they exist. We can do so like this:
<?php echo ereg("a{3,}$", "aaaa"); ?>
Repeat character zero or more times
To tell the regular expression engine that a character may exist, and can be repeated, we use
the *
character. Here are two examples that would return true:
<?php echo ereg("t*", "tom"); ?>
<?php echo ereg("t*", "fom"); ?>
Even though the second example doesn't contain the 't' character, it still returns true because
the *
indicates that the character may appear, and that it doesn't have to. In fact,
any normal string pattern would cause the second call to ereg
above to return true,
because the 't' character is optional.
Repeat character one or more times
To tell the regular expression engine that a character must exist and that it can be repeated
more than once, we use the +
character, like this:
<?php echo ereg("z+", "i like the zoo"); ?>
The following example would also return true:
<?php echo ereg("z+", "i like the zzzzzzoo!"); ?>
Repeat character zero or one times
We can also tell the regular expression engine that a character must either exist just once, or
not at all. We use the ?
character to do so, like this:
<?php echo ereg("c?", "cats are fuzzy"); ?>
If we wanted to, we could even entirely remove the 'c' from the search string shown above, and this expression would still return true. The '?' means that a 'c' may appear anywhere in the search string, but doesn't have to.
[previous] [next] |
Created: March 4, 2002
Revised: March 4, 2002
URL: https://webreference.com/programming/php/regexps/3.html