PHP - Regular Expression

A regular expression is a specially formatted pattern that can be used to find instances of one string within another.several programming languages including visual basic,perl,javascript and php support regular expression.in another words regular expressions are basically a pattern definition language used to make complex and flexible string searches possible.

A regular expression is used to checj email address,phone number,pin code,zip code,website url, so programmers are not required to develop their own complex and lengthy programming routine to do above task.

Type of Regular Expression

The php support two flavors of regular expression,Perl compatible regular expression(PCRE) and POSIX extended regular expression have both been available above php3.the following symbols used in regular expression.

Symbols Used in Regular Expression

The following symbols are used in regular expression.

Symbols

Description

^

The ^ Symbol indicates the start of a string for example "^Veewom" matches any string that start with Veewom.

$

The $ symbols indicates the end of a string for example "Veewom$" matches any string that ends with Veewom.

*

The symbols * denotes zero or more number of times a character or a sequence of characters may occur for example "ts*" matches a string that has a character t followed by zero or more s.

+

The symbol + denotes one or more number of times a character or a sequence of character may occur for example "ts+" matches a string that has a character s followed by at least one or more s.

?

The symbol ? denotes zeor or one character may occur for example "ts?" matches the string that may have a character t but may or may not be followed by single character s.

{}

Bounds can also be used which come inside braces and indicate ranges in the terms of occurrences for example "ts{2} matches a string that has character t followed by exactly two s.

()

To quantify a sequence of characters,put them inside round brackets for example "t(ss)*" matches a string that has a character t followed by zero or more copies of the sequence "ss".

|

The "|" symbol works similar to the OR operator for example "ts|pl" matches a string that holds either "ts" or "pl".

.

A period "." stands for any single character for example "t.[0-9]" matches a string that has a character s followed by any one character further followed by a digit.

[]

Bracket expression specify which characters are allowed in a single position of a string for example "[ts]" matches a string that has either character t or s.

Share Share on Facebook Share on Twitter Share on Google+ Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit