Sunday, 27 January 2013

Regular Expressions PHP/Javascript

MetaCharacter: It is special chacter(s) which have special meaning in Regular Expression.

For Example:
*, ., /, \, ? and ^.

Sr
Meta character
Desctiption
1
[ ]
Match any character within [phc]
Means find “p” OR “h” OR “c”
2
-
Range
a-z means a to z(a,b,c, ... z)
0-0 means 0 to 9 (0,1,2,3,4,5,6,7,8,9)
A-Z means A to Z(A,B,C,D ..... Z)
3
^
Caret It means start with a character
For example: ^ab (start with “a”)

Inside the bracket if have opposite meaning.
For example: [^a](must not start with “a”)

4
$
End with character
For Example
Abc$ means end with “c”
5
.
The . (period) means any character(s) in this position,
For example, ph. will find php, php-tutorial and php-tutorial-php but not aphp because it has no following character
6
?
Matches the preceding character 0 or 1 times only.
For example:
colou?r will find both color (0 times) and colour (1 time).
7
*
Matches the preceding character 0 or more times.
For example:
tre* will find tree (2 times) and tread (1 time) and trough (0 times).
8
+
Matches the previous character 1 or more times.
For example:
tre+ will find tree (2 times) and tread (1 time) but NOT trough (0 times).
9
{n}
Preceding character, or character range, n times exactly.
For example:
find a local phone number we could use [0-9]{3}-[0-9]{4} which would find any number of the form 723-4567 OR 132-3234 OR 214-3433.
10
()
To group a character.
For Example:
(php), will find the “php” only.
11
|
Vertical bar used for OR
(a|c) find either “a” OR “c”
12
\d
any character in the range 0 – 9
13
\D
Any character not in between 0-9
14
\s
Whitespace or tab
15
\S
Not whitespace or tab
16
\w
Alphanumeric characters (a-z, 0-9, A-Z)
17
\W
Not alphanumeric character (a-z, 0-9, A-Z)