You have read the first and second part already right? Now it get your
hand dirty with more regular expression in PHP lesson. In this lesson,
I will introdce you 4 more special characters that are used in regular
expressions.
The first I want to introduce is
?.
? mean optional. For example,
"me?n"
which mean letter e is optional. The string that match this expression,
it should be
"men" or
"mn" but not
"man".
() is also regular expression special character. Its job is to group characters
into a string. For simple example:
"^(ab)" match only with string that is
start with letter
ab. Another example:
"n(am)e" will only match with
"name" but not
"nae" or
"ne". Another useful example,
"^(ab)+" match with
string that contains one or more
"ab" at the beginning. Now let use
() and
?
together:
PHP Code:
$regexp = "^[A-Za-z]+([0-9]+)?$";
$string = "hello22";
if (ereg($regexp, $string)) {
print "Match...!";
} else {
print "Not Match...!";
}
Let analyze what this regular expression,
"^[A-Za-z]+([0-9]+)?$", does.
"^[A-Za-z]+" mean only string that start with alphabet character, and
"([0-9]+)?$" mean it is optional to have number at the end of the string.
So this expression will match with
"hello",
"hello2007" and
"man18" but
not match with
"L1fE" or
"se7en".
Full-stop
(.) mean any single character. For example,
".." match with
any string that contain at least 2 characters. Another example,
"^..$" match with any string that contain exactly 2 characters.
"............" there are 10 full-stops in this quote which mean only string
that contains at least 10 characters. But there are shorter way to do
it by using
{}.
{} mean repeating.
".{10}" is equal to
".........." and
"a{5}" is equal to
"aaaaa". But
{} give a powerful function that
allow us to determine the range of repeatation. For example:
"a{2,5}" mean it match to string that have at least 2 of letter
a but must
be less than 6 of
a letter.
I will write a very powerful email validation by using a very complex
regular expression:
PHP Code:
<?php
$regexp = "^[A-Za-z0-9._-]+@[A-Za-z0-9_-]+(\.[A-Za-z]+){1,2}$";
$string = "invisal@gmail.com";
if (ereg($regexp, $string)) {
print "Match...!";
} else {
print "Not Match...!";
}
?>
"^[A-Za-z0-9._-]+@[A-Za-z0-9_-]+(\.[A-Za-z]+){1,2}$" is match with
"invisal@gmail.com",
"invisal@programmestalk.net" and
"admin@admin.co.nr"
but not match with
"invisal@he.co.kh.gov" because it is not possible
to have
"he.co.kh.gov" domain name. Complicated? Let me explain,
"^[A-Za-z0-9._-]+@" allow only alphabet character, numbers and
".", "_" or
"-" at beginning of string until it reach character @.
"[A-Za-z0-9_-]+" for domain name only allow alphabet character, numbers,
"_" and
"-" until it reach it character
"." in here I used
"\." instead of
"."
because if I use only
".", the regular expression perform
"." as allow any
single character which is function of
"." do in regular expression. But
I use
\. mean I only need a simple fullstop character.
"(\.[A-Za-z]+){1,2}$"
match any string that end with
"." + alphabets and it could only be repeat
2 times. It match
".com",
".com.kh",
".us" and
"co.nr" but not match with
".com.net.org" or
"co.uk.com"....
That's all for the Basic Regular Expression, I hope you enjoy my tutorial
from the first part until the last part. If you don't understand any part
you can ask me questions. I glad to help

I. You can tell me, If you need
more useful regular expression, I will post it on this website
---------------- END --------------------------
From siLenTz
Regular expression journey is ending here....