Programming on Python. Lecture 7

Содержание

Слайд 2

Intro to Python for Data Science

Regular Expression

Intro to Python for Data Science Regular Expression

Слайд 4

WHAT IS A REGULAR EXPRESSION?

A Regular Expression (RegEx) is a sequence of characters that

WHAT IS A REGULAR EXPRESSION? A Regular Expression (RegEx) is a sequence
defines
a search pattern.
For example,
^a...s$
The above code defines a RegEx pattern.
The pattern is: any five letter string starting with a and ending with s.

Слайд 5

A pattern defined using RegEx can be used to match against a

A pattern defined using RegEx can be used to match against a string.
string.

Слайд 6

Python has a module named re to work with RegEx. Here's an example:

import

Python has a module named re to work with RegEx. Here's an
re
pattern = '^a...s$’
test_string = 'abyss’
result = re.match(pattern, test_string)
if result:
print("Search successful.")
else: print("Search unsuccessful.")

Here, we used re.match() function to search pattern within the test_string.
The method returns a match object if the search is successful. If not, it returns None.

Слайд 7

THERE ARE OTHER SEVERAL FUNCTIONS DEFINED IN THE RE MODULE TO WORK WITH REGEX.

THERE ARE OTHER SEVERAL FUNCTIONS DEFINED IN THE RE MODULE TO WORK
BEFORE WE EXPLORE THAT, LET'S LEARN ABOUT REGULAR EXPRESSIONS.

Слайд 8

SPECIFY PATTERN USING REGEX

To specify regular expressions, metacharacters are used.
In the

SPECIFY PATTERN USING REGEX To specify regular expressions, metacharacters are used. In
previous example, ^ and $ are metacharacters.

Слайд 9

METACHARACTERS

METACHARACTERS ARE CHARACTERS THAT ARE INTERPRETED IN A SPECIAL WAY BY A

METACHARACTERS METACHARACTERS ARE CHARACTERS THAT ARE INTERPRETED IN A SPECIAL WAY BY
REGEX ENGINE. HERE'S A LIST OF METACHARACTERS:
[] . ^ $ * + ? {} () \ |

Слайд 10

METACHARACTERS

[] - Square brackets

Square brackets specifies a set of characters you wish

METACHARACTERS [] - Square brackets Square brackets specifies a set of characters
to match.

Here, [abc] will match if the string you are trying to match contains any of the a, b or c.

Слайд 11

METACHARACTERS

You can also specify a range of characters using - inside square brackets.
[a-e] is the

METACHARACTERS You can also specify a range of characters using - inside
same as [abcde].
[1-4] is the same as [1234].
[0-39] is the same as [01239].
You can complement (invert) the character set by using caret ^ symbol at
the start of a square-bracket.
[^abc] means any character except a or b or c.
[^0-9] means any non-digit character.

Слайд 12

METACHARACTERS

. - Period
A period matches any single character (except newline '\n').

METACHARACTERS . - Period A period matches any single character (except newline '\n').

Слайд 13

METACHARACTERS

^ - Caret
The caret symbol ^ is used to check if a string starts with a certain character.

METACHARACTERS ^ - Caret The caret symbol ^ is used to check

Слайд 14

METACHARACTERS

$ - Dollar
The dollar symbol $ is used to check if a string ends with a certain character.

METACHARACTERS $ - Dollar The dollar symbol $ is used to check

Слайд 15

METACHARACTERS

* - Star
The star symbol * matches zero or more occurrences of the pattern left to it.

METACHARACTERS * - Star The star symbol * matches zero or more

Слайд 16

METACHARACTERS

+ - Plus
The plus symbol + matches one or more occurrences of the pattern left to it.

METACHARACTERS + - Plus The plus symbol + matches one or more

Слайд 17

METACHARACTERS

? - Question Mark
The question mark symbol ? matches zero or one occurrence of the pattern left to

METACHARACTERS ? - Question Mark The question mark symbol ? matches zero
it.

Слайд 18

METACHARACTERS

{} - Braces
Consider this code: {n,m}.
This means at least n, and at most m repetitions of the

METACHARACTERS {} - Braces Consider this code: {n,m}. This means at least
pattern left to it.

Слайд 19

METACHARACTERS

Let's try one more example. This RegEx [0-9]{2, 4} matches at least 2 digits

METACHARACTERS Let's try one more example. This RegEx [0-9]{2, 4} matches at
but not more than 4 digits

Слайд 20

METACHARACTERS

| - Alternation
Vertical bar | is used for alternation (or operator).

Here, a|b match any string that contains either a or b

METACHARACTERS | - Alternation Vertical bar | is used for alternation (or

Слайд 21

METACHARACTERS

() - Group
Parentheses () is used to group sub-patterns. For example, (a|b|c)xz match any string that
matches either a or b or c followed

METACHARACTERS () - Group Parentheses () is used to group sub-patterns. For
by xz

Слайд 22

METACHARACTERS

\ - Backslash
Backlash \ is used to escape various characters including all metacharacters.
For example,
\$a match if

METACHARACTERS \ - Backslash Backlash \ is used to escape various characters
a string contains $ followed by a. Here, $ is not interpreted by a RegEx engine in a special way.
If you are unsure if a character has special meaning or not, you can put \ in front of it. This makes sure the character is not treated in a special way.

Слайд 23

SPECIAL SEQUENCES

Special sequences make commonly used patterns easier to write. Here's a

SPECIAL SEQUENCES Special sequences make commonly used patterns easier to write. Here's
list of special sequences:

\A - Matches if the specified characters are at the start of a string.

Слайд 24

SPECIAL SEQUENCES

\b - Matches if the specified characters are at the beginning or

SPECIAL SEQUENCES \b - Matches if the specified characters are at the
end of a word.

Слайд 25

SPECIAL SEQUENCES

\B - Opposite of \b. Matches if the specified characters are not at the beginning

SPECIAL SEQUENCES \B - Opposite of \b. Matches if the specified characters
or end of a word.

Слайд 26

SPECIAL SEQUENCES

\d - Matches any decimal digit. Equivalent to [0-9]

\D - Matches any non-decimal

SPECIAL SEQUENCES \d - Matches any decimal digit. Equivalent to [0-9] \D
digit. Equivalent to [^0-9]

Слайд 27

SPECIAL SEQUENCES

\s - Matches where a string contains any whitespace character. Equivalent to [

SPECIAL SEQUENCES \s - Matches where a string contains any whitespace character.
\t\n\r\f\v].

\S - Matches where a string contains any non-whitespace character. Equivalent to [^ \t\n\r\f\v].

Слайд 28

SPECIAL SEQUENCES

\w - Matches any alphanumeric character (digits and alphabets). Equivalent to [a-zA-Z0-9_].
By

SPECIAL SEQUENCES \w - Matches any alphanumeric character (digits and alphabets). Equivalent
the way, underscore _ is also considered an alphanumeric character.

\W - Matches any non-alphanumeric character. Equivalent to [^a-zA-Z0-9_]

Слайд 29

SPECIAL SEQUENCES

\Z - Matches if the specified characters are at the end of

SPECIAL SEQUENCES \Z - Matches if the specified characters are at the end of a string.
a string.

Слайд 30

SPECIAL SEQUENCES

Tip: To build and test regular expressions, you can use RegEx tester

SPECIAL SEQUENCES Tip: To build and test regular expressions, you can use
tools such as regex101.com. This tool not only helps you in creating regular expressions, but it also helps you learn it.
Now we understand the basics of RegEx, let’s learn how to use RegEx in Python code.

Слайд 31

PYTHON REGEX

Python has a module named re to work with regular expressions.
To use

PYTHON REGEX Python has a module named re to work with regular
it, we need to import the module.
import re
The module defines several functions and constants to work with RegEx.

Слайд 32

PYTHON REGEX

re.findall()
The re.findall() method returns a list of strings containing all matches.

Example 1: re.findall()
#

PYTHON REGEX re.findall() The re.findall() method returns a list of strings containing
Program to extract numbers from a string
import re
string = 'hello 12 hi 89. Howdy 34’
pattern = '\d+’
result = re.findall(pattern, string)
print(result) # Output: ['12', '89', '34’]
If the pattern is not found, re.findall() returns an empty list.

Слайд 33

PYTHON REGEX

re.split()
The re.split method splits the string where there is a match and returns

PYTHON REGEX re.split() The re.split method splits the string where there is
a list of strings
where the splits have occurred.

Example 2: re.split()
import re
string = 'Twelve:12 Eighty nine:89.’
pattern = '\d+’
result = re.split(pattern, string)
print(result)
# Output: ['Twelve:', ' Eighty nine:', '.’]
If the pattern is not found, re.split() returns a list containing the original string.

Слайд 34

PYTHON REGEX

You can pass maxsplit argument to the re.split() method. It's the maximum number of splits

PYTHON REGEX You can pass maxsplit argument to the re.split() method. It's
that will
occur.
import re
string = 'Twelve:12 Eighty nine:89 Nine:9.’
pattern = '\d+' # maxsplit = 1 # split only at the first occurrence
result = re.split(pattern, string, 1)
print(result)
# Output: ['Twelve:', ' Eighty nine:89 Nine:9.’]
By the way, the default value of maxsplit is 0; meaning all possible splits.

Слайд 35

PYTHON REGEX

re.sub()
The syntax of re.sub() is:
re.sub(pattern, replace, string)
The method returns a string where matched

PYTHON REGEX re.sub() The syntax of re.sub() is: re.sub(pattern, replace, string) The
occurrences are replaced with the content of replace variable.

Слайд 36

PYTHON REGEX

Example 3: re.sub()
# Program to remove all whitespaces
import re
#

PYTHON REGEX Example 3: re.sub() # Program to remove all whitespaces import
multiline string
string = 'abc 12\ de 23 \n f45 6’
# matches all whitespace characters
pattern = '\s+’
# empty string
replace = ‘’
new_string = re.sub(pattern, replace, string)
print(new_string)
# Output: abc12de23f456
If the pattern is not found, re.sub() returns the original string.

Слайд 37

PYTHON REGEX

You can pass count as a fourth parameter to the re.sub() method.
If omitted, it

PYTHON REGEX You can pass count as a fourth parameter to the
results to 0. This will replace all occurrences.
import re
# multiline string
string = 'abc 12\ de 23 \n f45 6’
# matches all whitespace characters
pattern = '\s+’
replace = ‘’
new_string = re.sub(pattern, replace, string, 1)
print(new_string)
# Output:
# abc12\ de 23
# f45 6

Слайд 38

PYTHON REGEX

re.subn()
The re.subn() is similar to re.sub() expect it returns a tuple of 2 items containing

PYTHON REGEX re.subn() The re.subn() is similar to re.sub() expect it returns
the
new string and the number of substitutions made.
Example 4: re.subn()
# Program to remove all whitespaces
import re
# multiline string
string = 'abc 12\ de 23 \n f45 6’
# matches all whitespace characters
pattern = '\s+’
# empty string
replace = ‘’
new_string = re.subn(pattern, replace, string)
print(new_string)
# Output: ('abc12de23f456', 4)

Слайд 39

PYTHON REGEX

re.search()
The re.search() method takes two arguments: a pattern and a string.
The method

PYTHON REGEX re.search() The re.search() method takes two arguments: a pattern and
looks for the first location where the RegEx pattern produces a match
with the string.
If the search is successful, re.search() returns a match object; if not, it returns None.
match = re.search(pattern, str)

Слайд 40

PYTHON REGEX

Example 5: re.search()
import re
string = "Python is fun"
# check

PYTHON REGEX Example 5: re.search() import re string = "Python is fun"
if 'Python' is at the beginning
match = re.search('\APython', string)
if match:
print("pattern found inside the string")
else:
print("pattern not found")
# Output: pattern found inside the string
Here, match contains a match object.

Слайд 41

MATCH OBJECT

You can get methods and attributes of a match object using dir() function.
Some

MATCH OBJECT You can get methods and attributes of a match object
of the commonly used methods and attributes of match objects are:

match.group()
The group() method returns the part of the string where there is a match.
Example 6: Match object
import re string = '39801 356, 2102 1111’
# Three digit number followed by space followed by two digit number
pattern = '(\d{3}) (\d{2})’
# match variable contains a Match object.
match = re.search(pattern, string)
if match:
print(match.group())
else: print("pattern not found")
# Output: 801 35

Here, match variable contains a match object.

Слайд 42

MATCH OBJECT

match.start(), match.end() and match.span()
The start() function returns the index of the start of

MATCH OBJECT match.start(), match.end() and match.span() The start() function returns the index
the matched substring.
Similarly, end() returns the end index of the matched substring.
>>> match.start()
2
>>> match.end()
8
The span() function returns a tuple containing start and end index of the matched part.
>>> match.span()
(2, 8)

Слайд 43

MATCH OBJECT

match.re and match.string
The re attribute of a matched object returns a regular expression

MATCH OBJECT match.re and match.string The re attribute of a matched object
object.
Similarly, string attribute returns the passed string.
>>> match.re
re.compile('(\\d{3}) (\\d{2})’)
>>> match.string
'39801 356, 2102 1111'

Слайд 44

USING R PREFIX BEFORE REGEX

When r or R prefix is used before a regular expression, it

USING R PREFIX BEFORE REGEX When r or R prefix is used
means raw string. For example, '\n' is a new line whereas r'\n' means two characters: a backslash \ followed by n.
Backlash \ is used to escape various characters including all metacharacters. However, using r prefix makes \ treat as a normal character.

Example 7: Raw string using r prefix
import re
string = '\n and \r are escape sequences.’
result = re.findall(r'[\n\r]', string)
print(result)
# Output: ['\n', '\r']

Имя файла: Programming-on-Python.-Lecture-7.pptx
Количество просмотров: 45
Количество скачиваний: 0