Regular Expression Rule & Example

Post Date: Dec 12 2009
Total Visit: 4101

The regular expression, as a pattern, can match all kinds of text strings helping your application validate, compare, compute, decide etc. It can do simple or very complex string manipulations. The list of possibilities is enormous when it comes to what you can achieve using regular expressions. You can take any phrase that starts with an "A" or any A - Z character and do various things with it. You can match phone numbers, email addresses, URL, credit card numbers, social security numbers, zip codes, states, cities.....(the list never ends). A huge script that is supposed to validate a user input and prepare it for SQL can be reduced to only one line with the help of preg_replace.

There are two types of regular expression functions in PHP:

  1. ereg functions or POSIX extended regular expression function, which is the standard functions for PHP (i.e. ereg, ereg_replace, eregi, eregi_replace)
  2. preg functions or perl Compatible regular expressions (i.e. preg_grep, preg_match, preg_match_all(), preg_replace, preg_replace, preg_replace_callback(), preg_split).

Some basic Regular Expression Rules are as below:

^             Start of String
$        End of string
n*        Zero or more of 'n'
n+        One or more of 'n'
n?        A possible 'n'
n{2}        Exactly two of 'n'
n{2,}        At least 2 or more of 'n'
n{2,4}        From 2 to 4 of 'n'
()        Parenthesis to group substrings
(n|a)        Either 'n' or 'a'
.        Any single character
[1-7]        A number between 1 and 7
[b-t]        A lower case character between b and t
[B-T]         An upper case character between B and T
[^a-z]        Absence of lower case a to z
[_a-zA-Z]    An underscore or any letter of the alpha
\w        Any upper or lower case letter or number or underscore
\W        Any character not in \w
\d        Any digit 0-9
\D        Any character not in \d
i        A modifier - goes at the end of the pattern string - ignore case

Here is the PHP source code for email validation using PHP regx function:

$email = "";
if (ereg('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+.([a-zA-Z]{2,4})$',$email)
{ echo 'Valid email id';}
{echo 'Invalid email id';}

Now I am going to split the string  pattern into 3 parts, ie

Here ^ symbol denotes that this is  the start of the email part.
a-zA-Z0-9._- denotes combination of characters to form the username section of  an email
+ symbol denotes, it should have 1 or more characters from the proceeding sets
@ symbol is a default symbol between username and domain name parts.

a-zA-Z0-9._- denotes combination of characters to form the domain name without  the tld
+ symbol denotes, it should have 1 or more characters from the proceeding sets
. symbol denotes, the dot operator proceeding to the tld, here we are using to  escape

a-zA-Z denotes the combination of chars to form  the tld name
{2,4} denotes the length of the tld may be between 2 to 4 characters
$ denotes the end of an email.

Tags: Regex, Basic, Beginner, Validation, Rule Bookmark and Share

To do articles more effectively, we need your comments or suggestions on how to improve the articles and make it more useful, or what other articles you would like in the future. Please CLICK HERE and leave your feedback. Thanks!!!