What is Regular Expression
In simple words it is a pattern of symbol and characters. Regular expression is used to search,replace or modify the words/strings. It is the most important part of your daily programming usage whether rewriting url using .htaccess, form validation, validating user input etc.
Regular expression is a sequence of characters and each characters has it’s special meaning. Let’s look at some basic characters and it’s meaning.
Characters With Special Meaning in Regular Expression
. – Matches any character, except line breaks.
^ – Beginning of the string.
$ – It matches the end of the string.
? – It make preceding token optional.
\ – Escape special symbols.
+ – Matches one or more of the preceding token.
\d – Matches any character that is digit.
{2} – Matches exactly two of the preceding token. If i write \d{2}, it matches only two digit.
[a-z] – Any lowercase characters in the range a to z.
[A-Z] – Matches any uppercase characters in the range A to Z.
[0-9] – Matches any digit in the range 0 to 9.
Using above tokens let’s make some regular expression
1 |
$pattern = "/regex/"; |
Match regex in a string
1 |
$pattern = "/[rgx]/"; |
We write rgx in square bracket here.It Matches either r or g or x in a string.
* NOTE – Regular expression is a case sensitive so above pattern won’t match R,G and X.
To ignore case –
1 2 3 |
// Ignore case $pattern = "/[rgx]/i"; // It matches both lower and uppercase |
1 2 3 |
// Match globally $pattern = "/regex/g"; |
It matches every occurrence of regex in a page. g stands for global.
1 |
$pattern = "/^regex/"; |
Match any string string start with regex.
1 |
$pattern = "/^regex$/"; |
Matches any string start and end with regex. Means string only contain regex.
1 |
$pattern = "/regex?/"; |
Matches rege and regex both. x is optional.
1 |
$pattern = "/regex+/"; |
It matches (regex,regexxxxxx etc.). + is a greedy match.
1 |
$pattern = "/\d{10}/"; |
Checking 10 digit number.
Let’s take some problem statement
1. Validating a user input which contains lowercase letters,number,underscores and hypens and it should not less than 6 characters and not greater than 8 characters.
To write this pattern
1 |
$pattern = "/^[a-z0-9_-]{6,8}$/"; |
2. Suppose you want to match a number which contains three pairs. Each pair is separated by hypen and each pair has a length of 3,4,4.
123-4567-9876
We need to match digit separated by hypen
1 |
$pattern = "/\d{3}-\d{4}-\d{4}/"; |
preg_match function in PHP
preg_match is a function in PHP which performs regular expression match. Return 1 if it founds the match otherwise 0.
syntax –
1 |
preg_match ( string $pattern , string $inputString [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] ) |
1 2 3 4 5 6 7 8 9 10 |
<?php //[0-9] matches any digit in the range 0 to 9 if (preg_match("/[0-9]/","Hello i am looking for gate no 9",$matches)){ print_r($matches); } ?> |
Top Five most used PHP functions
Tools for Regular Expression
Regexr is one of my favourite tool to check regular expression. In the right section of regexr, it shows all regular expression characters and it’s meaning. It provides you the interface to write your regex, check the syntax before putting the regex in your code.
To know more about the history of regular expression check wikipedia article
Regular Expression Best Books
Regular Expression Book on Amazon