PYTHON ASSIGNMENT
REGULAR EXPRESSIONS
REG
NO: 22BRS1316
NAME:
K DEVAYANI
A Reg Ex, or Regular Expression, is a sequence of characters that forms a
search pattern. Reg Ex can be used to check if a string contains the specified
search pattern
Python has a built-in package called re, which can be used to work with
Regular Expression
REG EX FUNCTIONS
Python has a module named re to work with regular expressions. To use it, we
need to import the module.
import re
The module defines several functions and constants to work with RegEx.
[Link]()
The [Link]() method returns a list of strings containing all matches.
EXAMPLE:
The [Link] method splits the string where there is a match and returns a
list of strings where the splits have occurred.
If the pattern is not found, [Link]() returns an empty list.
RE. SEARCH()
The [Link]() function searches the string for a match, and returns a
match object if there is a match
If there is more than one match, only first occurrence of the match will be
returned:
[Link]()
The [Link] method splits the string where there is a match and returns a list
of strings where the splits have occurred.
If the pattern is not found, [Link]() returns a list containing the original string
[Link]()
The sub() function replaces the matches with the text of our choice
META CHARACTERS IN REGULAR
EXPRESSION
Metacharacters are characters that are interpreted in a special way by a
RegEx engine. Here's a list of metacharacters:
[] . ^ $ * + ? \ |
[] – SQUARE BRACKETS
Square brackets specifies a set of characters you wish to match.
. -PERIOD
A period matches any single character (except newline '\n').
^ - CARET
The caret symbol ^ is used to check if a string starts with a certain
character.
$ - DOLLAR
The dollar symbol $ is used to check if a string ends with a certain
character.
* - STAR
The star symbol * matches zero or more occurrences of the pattern left to
it.
+ - PLUS
The plus symbol + matches one or more occurrences of the pattern
left to it.
? – QUESTION MARK
The question mark symbol ? matches zero or one occurrence of the
pattern left to it.
| - ALTERNATION
Vertical bar | is used for alternation (or operator).
\ - BACKSLASH
Backlash \ is used to escape various characters including all
metacharacters.
SPECIAL SEQUENCES
Special sequences make commonly used patterns easier to write. Here's
a list of special sequences:
\A- Matches if the specified characters are at the start of a
string.
\b- Matches if the specified characters are at the beginning or
end of a word.
\B - Opposite of \b. Matches if the specified characters
are not at the beginning or end of a word.
\d - Matches any decimal digit. Equivalent to [0-9]
\D - Matches any non-decimal digit. Equivalent to [^0-9]
\s - Matches where a string contains any whitespace
character. Equivalent to [ \t\n\r\f\v].
\S - Matches where a string contains any non-whitespace
character. Equivalent to [^ \t\n\r\f\v].
\w - Matches any alphanumeric character (digits and
alphabets). Equivalent to [a-zA-Z0-9_]. By the way,
underscore _ is also considered an alphanumeric character.
\W - Matches any non-alphanumeric character. Equivalent to
[^a-zA-Z0-9_]
\Z - Matches if the specified characters are at the end of a
string.