The regular expressions or regex as they are known  popularly are used for string handling purpose.

If we want to compare the two strings, if we want to replace some of the characters in the string, if we want to extract specific pattern in a particular string, then we go for regex.

This regex enables us to get the required information in very easy way.

Regex is always applied to the strings only. The simples regex is a dot(.) which matches with “a” or “z” or “1”.

RULES FOR THE REGULAR EXPRESSIONS

The standard set of rules or matching symbols can be expressed in the form of following tables

Table No. 1

 

. Matches any character
^refex Matches regex that must match at the

beginning of the line

abc$ Matches regex that resides at the end of

the line

[abc] Matches either a or b or c
[abc][yz] Matches a or b or c followed by either y or

z

[^abc] Matches all the string that do not start with

either a or b or c

[a-z0-9] This depicts the range(-) in regex. It matches

any character from a to z followed by any integer

from 0 to 9

a|b Matches either a or b
ab Matches a followed by b only
$ Matches the end of the line i.e. blank line

 

Table No. 2

\d Matches any digit, short for [0-9]
\D Matches any non-digit, short for [^0-9]
\s Matches any whitespace character, short for

[ \t\n\r\f]

\S Matches any non-whitespace character,

short for [^\s]

\w Matches any word character, short for [a-

zA-Z0-9]

\W Matches any non-word character, short for

[^\w]

\S+ Matches several non-whitespace character
\b Matches a word boundary

 

Table No. 3

 

* Occurences of zero or more times, short for

{0,}

+ Occurences of one or more times, short for

{1,}

? Occurences of none or one times, short for

{0,1}

{x} Occurs x number of times
{x,y} Occurs between x and y times

 

In the upcoming blogs on regex, grouping concept will be discussed.
Hope you had a great read. Thank you.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s