Regular Expression Rule & Example

December 12, 2009

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:

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

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.

Post a Comment

Your email address will not be published. Required fields are marked *


  • Recent Posts

    • Secure Your CodeIgniter Application using CSRF Token

      by admin
      In this tutorial, we will learn how to make our website more secure & protect from CSRF when we are using CodeIgniter web application framework. We thanks CodeIgniter team who giving in built support Cross Site Rquest Forgery (CSRF or XSRF). To enable CSRF protection CodeIgniter Framework will automatically protect forms or AJAX calls from CSRF when […]
    • Multiple Themes in CodeIgniter like WordPress

      by admin
      Create multiple themes and select one of them from backend like wordpress is drawbacks of a framework. Similar CodeIgniter have not an option to select one theme from multiple theme concept but we can re arrange using CodeIgniter template concept. We are going to handle multiple themes in a very simple way, in this example […]
    • How to create REST API in PHP?

      by admin
      REST (Representational State Transfer) is the standard design architecture for developing web services API. It is simple easy to understand and developing client-server relationship API. REST takes advantage of the HTTP request methods to layer itself into the existing HTTP architecture. We can handle GET, PUT, POST, DELETE operations through it. A REST API allows […]
    • Basic security vulnerabilities in php code

      by admin
      Today I have informed PHP programmers of common security mistakes that can be overlooked in PHP scripts. In the beginning programmers fail to understand about the PHP security issues or how to make secure script. The wise programmer knows that the real question is how secure a site is. Here I have focus how to […]
    • Generating CAPTCHA Image Using PHP

      by admin
      The CAPTCHA is a very very useful test to prevent abuse on the websites. When you create a web form like registration, login, contact us, blog comment etc…, We are suffering day by day with unwanted email or web spam abuse. So if you use CAPTCHA on your website forms, this can help in stopping […]
Copyright © 2012-2013 Sujit Shah. .
Tags: PHP Programmer Nepal, Web Developer Nepal, Website Designer, CodeIgniter Developer, Yii Framework, Wordpres, OpenCart, Drupal, Front End Developer, Responsive Developer, Bootstrap, Foundation, Freelance PHP MySql Programmer Nepal, Expert Programmer Nepal, PHP, MySql, LAMP, Linux, Apache, SVN, JavaScript, Ajax, jQuery, HTML, HTML5, CSS, CSS3, XML, SOAP, PSD to XHTML, Responsive Web Design, Web Development, CMS, E-commerce, Classified, Job portal, Travel & tours, Penny Auction, Lowest Unique Bid Auction, Reverse Auction, Price Reveal Auction, Payment Gateway Integration, Nepal, India, Hong Kong, Australia, UK, USA, Singapore, Germany, Canada, Netherlands, New Zealand, Norway, Italy