Saturday, 6 August 2016

14.REGULAR EXPRESSIONS IN PYTHON

Regular Expressions

1)* -> it matches zero or more occurances of preceding character
*->ab*c
Ac
Abc
Abbc
Abbbbbc
2)+ ->it matches one or more occurrences of preceding character
+ -> ab+c
Ac #
Abc
Abbc
Abbbbbc
3)? ->it matches zero or one occurance of preceding character
? ->ab?c
Ac
Abc
Abbc #
Perl,pearl => pea?rl
Color,colour=>colou?r
4). ->it matches any single character
. -> a.c
Agc
A5c
A$c
A c
Abcd #
5)[] ->it matches any single character in the given list
[xyz…]->b[aeiou]d
Bad
Bed
Bid
Bod
Bud
B8d #
Bpd #
6)[^]->it matches any single character other than in the give list
[^xyz…]->b[^aeiou]d
Bad #
Bed #
Bid #
Bod #
Bud #
B8d
Bpd
7) [-]->it matches any single character in the given range
z[a-e]y
xay
xby
xcy
xdy
xey
xfy #
xpy #
[0-9]->any single digit
[a-z]->any one lowercase alphabet
[A-Z]->any one uppercase alphabet
[a-zA-Z]->any one alphabet
[a-zA-Z0-9_]->any one alphanumeric
[^0-9]->any single non digit
[^a-z]->any one non lowercase alphabet
[^A-Z]->any one non uppercase alphabet
[^a-zA-Z]->any one non alphabet
[^a-zA-Z0-9_]->any one non alphanumeric(special characters)
8)(|)->match any one string in the list
(java|hadoop|python)
9) {m}->it matches exact occurrence of preceding character
Ab{3}c
Abbc #
Abbbc
Abbbbbc #
10) {m,n} -> it matches min m occurrences and max n occurrences of ist preceding character
Ab{3,5}c
Abbc #
Abbbc
Abbbbc
Abbbbbc
Abbbbbbc #
11) {m,}-> it matches min m occurrences and max no limit of its preceding character
Ab{3,}c
Abbc #
Abbbc
Abbbbbbbc
12) ^ ->start of the line
^perl
^[abc]
^[^abc]
13) $ ->end of the line
Perl$
[0-9]$
13) \d or [0-9] ->any single digit
[0-9][0-9][0-9][0-9] or [0-9]{4} or
\d\d\d\d or \d{4}
14) \D or [^0-9] -> any single non digit
15) \w or [a-zA-Z0-9_] ->any alphanumeric
16) \W or [^a-zA-Z0-9_]-> any non alphanumeric or special character
17)\s =>’ ‘,’\t’,’\n’
18)\b =>word boundry





No comments:

Post a Comment