[go: up one dir, main page]

0% found this document useful (0 votes)
9 views4 pages

Lecture 14 Regular Expressions

The document provides an overview of regular expressions (regex) in Python, detailing their purpose and the use of the re module for pattern matching. It explains the match function and its syntax, as well as various regex characters and their meanings with examples. Additionally, it describes special sequences and character classes used in regex for more precise string matching.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODP, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Lecture 14 Regular Expressions

The document provides an overview of regular expressions (regex) in Python, detailing their purpose and the use of the re module for pattern matching. It explains the match function and its syntax, as well as various regex characters and their meanings with examples. Additionally, it describes special sequences and character classes used in regex for more precise string matching.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODP, PDF, TXT or read online on Scribd
You are on page 1/ 4

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]"

\ Signals a special sequence (can also be used to "\d"


escape special characters)
. Any character (except newline character) "he..o"

^ Starts with "^hello"


$ Ends with "planet$"
* Zero or more occurrences "he.*o"
+ One or more occurrences "he.+o"

? Zero or one occurrences "he.?o"


{} Exactly the specified number of occurrences "he.{2}o"

| 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"

You might also like