Regular Expression (Regex) Tutorial, Tools, Tester, Syntax

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

Match regex in a string

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 –

It matches every occurrence of regex in a page. g stands for global.

Match any string string start with regex.

Matches any string start and end with regex. Means string only contain regex.

Matches rege and regex both. x is optional.

It matches (regex,regexxxxxx etc.). + is a greedy match.

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

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

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 –

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.

regex

To know more about the history of regular expression check wikipedia article

Regular Expression Best Books

Regular Expression Book on Amazon

Regular Expression Cook Book on Flipkart

Regular Expression Books on Amazon India

Tagged , , . Bookmark the permalink.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.