View Single Post
  #1 (permalink)  
Old 06-28-2007, 11:00 AM
siLenTz's Avatar
siLenTz siLenTz is offline
Jr. Programmer
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jun 2007
Posts: 87
iTrader: (0)
siLenTz will become famous soon enoughsiLenTz will become famous soon enough
Regular Expressions In PHP (Part 3)

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....

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by siLenTz : 06-28-2007 at 11:05 AM.
Reply With Quote
The Following 3 Users Say Thank You to siLenTz For This Useful Post:
HelloWorld (06-28-2007), Lee (06-28-2007), TeraTask (06-29-2007)