Lecture 14 Regular Expressions
Lecture 14 Regular Expressions
●
A regular expression is a special sequence of
characters that helps you match or find other
strings or sets of strings, using a specialized
syntax held in a pattern.
●
The module re provides full support for Perl-like
regular expressions in Python. The re module
raises the exception re.error if an error occurs
while compiling or using a regular expression.
The match Function
●
This function attempts to match RE pattern to
string with optional flags.
●
Here is the syntax for this function −
– re.match(pattern, string, flags = 0)
– Where pattern=the regular expression to be
matched.
– String=the string, which would be searched to
match the pattern at the beginning of string.
– flags= specify different flags using bitwise OR (|).
Character Description Example
[] A set of characters "[a-m]"
| Either or "falls|stays"
() Capture and group
Character Description Example
\A Returns a match if the specified characters are at the beginning of "\AThe"
the string
\b Returns a match where the specified characters are at the r"\bain"
beginning or at the end of a word r"ain\b"
(the "r" in the beginning is making sure that the string is being
treated as a "raw string")
\B Returns a match where the specified characters are present, but r"\Bain"
NOT at the beginning (or at the end) of a word (the "r" in the r"ain\B"
beginning is making sure that the string is being treated as a "raw
string")
\d Returns a match where the string contains digits (numbers from 0- "\d"
9)
\D Returns a match where the string DOES NOT contain digits "\D"
\s Returns a match where the string contains a white space character "\s"
\S Returns a match where the string DOES NOT contain a white space "\S"
character
\w Returns a match where the string contains any word characters "\w"
(characters from a to Z, digits from 0-9, and the underscore _
character)
\W Returns a match where the string DOES NOT contain any word "\W"
characters
\Z Returns a match if the specified characters are at the end of the "Spain\Z"