Regular Expressions in PYTHON


Regular expressions are patterns used to match character combinations. Look at given below table:

Regular Expressions


foo.* # Matches any string starting with foo
\d* # Match any number decimal digits
[a-zA-Z]+ # Match a sequence of one or more letters
text Match literal text
. Match any character except newline
^ Match the start of a string
$ Match the end of a string
* Match 0 or more repetitions
+ Match 1 or more repetitions
? Match 0 or 1 repetitions *? Match 0 or more, few as possible
+? Match 1 or more, few as possible
{m,n} Match m to n repetitions
{m,n}? Match m to n repetitions, few as possible
[...] Match a set of characters
[^...] Match characters not in set
A | B Match A or B (...) Match regex in parenthesis as a group
\number Matches text matched by previous group
\A Matches start of string
\b Matches empty string at beginning or end of word
\B Matches empty string not at begin or end of word
\d Matches any decimal digit
\D Matches any non-digit
\s Matches any whitespace
\S Matches any non-whitespace
\w Matches any alphanumeric character
\W Matches characters not in
\w \Z Match at end of string.
\\ Literal backslash