Thursday, July 30, 2009

What is regular expression or regex or regexp

Regular expression or in other words regex or regexp is a pattern that describes particular characters or words or patterns of characters. Regular expressions provide very flexible way to to express a set of characters that we are interest of.

You may have questions about why regular expression comes? What is the use of it? To answer it, let's look a simple example below.

You have a written large piece of code in your development environment where is used upper version of php and the code contains following words.
$stmt->bindParam(':userName', $userName);
$stmt->bindParam(':password', $password);
$stmt->bindParam(':firstName', $firstName);
$stmt->bindParam(':lastName', $lastName);
$stmt->bindParam(':companyName', $companyName);

There exists this types of statements many times and many places in your code. Suppose you have 40,000 lines of code and out of 40,000 lines you have this type of statements in 5,000 places. Now in your production you see php version there does not support bindparam function. So what you need to do? You need to search all 40,000 lines and find the 5,000 places where bindparam function exists and then replace the function with suitable function that is supported by that version as well as corresponding syntax of that function.

In this example we are needed to change all $stmt->bindParam by mysql_real_escape_string as well as we don't need bind variable and their quote, for example we exactly need to look as
mysql_real_escape_string( $userName);
mysql_real_escape_string( $password);
mysql_real_escape_string( $firstName);
mysql_real_escape_string( $lastName);
mysql_real_escape_string( $companyName);


In this scenario with using regular expression we can save a lots of time just using a single expression wise search and replace command. If you just do search
$stmt-<bindParam(':any_character_here',
and replace them by
mysql_real_escape_string(
then our goal will be achieved. Regular expression does the trick, search for pattern
$stmt-<bindParam('.*',
and then replace them by,
mysql_real_escape_string(

where .* combination denotes regular expression.
-Dot(.) indicates matches any character except a newline.
-* indicates matches the preceding pattern element zero or more times.


So if there is such pattern found like
first comes $stmt-<bindParam('
and finish by ', which is followed by any combination of characters that to be replaced by mysql_real_escape_string(

We see here one command will do all 5,000 searches and replaces instead of searching/typing those.

We also need regular expression to express certain types of patterns while writing something. Like someone wrote , "I love grey|gray color". Here | is regular expression which indicates any of grey or gray.

This is all about regular expression, their usage. In subsequent post I will go more with regular expressions.

Related Documents

How to add a word or letter at the end of each line in shell script


How to add a line to the first in a file using shell script

No comments:

Post a Comment