Wednesday, September 5, 2012

Regular Expressions

Regular Expressions is an important part of development. Although it seems simple but to write complex regular expressions is very difficult task. Regular Expression are used to validate data against it and also used for pattern matching and for searching and replacing of strings.

I am providing a facility here by which you can validate expressions also.


Regular Expression: Test String:

Result:


Below are the list of characters which are used in Regular Expressions for creating Regular Expressions.

Wildcard Character Description
* Zero or more occurrences of the previous character or sub-expression.

For Example:
PREV*included

Matched:
included
PREVincluded

Not Matched:
PREVPREVincluded
(Although Description says sub-expression could occur multiple time but actually it can occur only single time like in below example.)

Second Example:
a*included

Matched:
included
aincluded
aaincluded
aaaincluded

Not Matched:
abincluded
aacincluded
aaabhdgincluded

+
One or more occurrences of the previous character or sub-expression.

For Example:
PREV+included

Matched:
PREVincluded

Not Matched:
included

PREVPREVincluded
(Although Description says sub-expression could occur multiple times but actually it can occur only single time like in below example.)


Second Example:
a+included

Matched:
aincluded
aaincluded
aaaincluded

Not Matched:
included
aacincluded
aaabhdgincluded


()
Groups a sub-expression that will be treated as a single element.

For Example:
(SingleElement)+

Matched:

SingleElement
SingleElementSingleElement
SingleElementSingleElementSingleElement
SingleElementSingleElementSingleElementSingleElement
and many more like above.
| Its same like OR operator which takes two operands and match any one operand or both.

For Example:
First|Second

Matched:
First
Second

Second Example:
http|www|ftp

Matched:
http
httpwww
httpwwwftp
httpftpwww

{}
Matches one character in a range of valid characters.

For Example:
[A-D]

Matched:
A
ABCD
AF
B12

Not Matched:
NF
KJ
12
12
$#

[^]
Matches a character that isn’t in the given range.

For Example:
[^A-C]

Matched:
D
D1
123abc
abc
and many more like above.

Not Matched:
A
B
C


.
Any character except newline means instead of dot (.) character you can use any character but you cannot find matches in multiple lines.

For Example:
.word

Matched:
aword
bword
Aword
1word
#word
and many more like above.

Not Matched:
word
abword
12word
AB12dvfword
#$word

Could not match in multiple lines.

\s
Any white-space character (such as a Tab or Space).

For Example:
Space Character
Tab Character
New Line Character
Carriage Return
Form Feed Character
Vertical Tab Character


\S
Any non white-space character.

For Example:

Matched:
Any alphanumeric or special character

Not Matched:
Space Character
Tab Character
New Line Character
Carriage Return
Form Feed Character
Vertical Tab Character


\d
Any digit character.

For Example:
\d

Matched:
Match any character from 0-9

Not Matched:
Any alphabetic character or special character.


\D
Any character that isn’t a digit.

For Example:
\D

Matced:
Match any alphabetic or special character.

Not Matched:
Any character from 0-9


\w
Any “word” character (letter, number, or underscore).

For Example:
\w

Matched:
A-Z, a-z, 0-9 or underscore

Not Matched:
Special characters except underscore.



\W
Any non word character.

For Example:
\W

Matched:
%
$
#
any other character which are not in Not Matched category below.

Not Matched:
A-Z, a-z, 0-9


Hope you have found the post useful. I am grateful for your positive feedback to improve.


No comments:

Post a Comment