Python
/
Strings
- 1 Language 9
-
Hello World S
-
Variables S
-
Functions S
-
Conditional A S
-
Operators S
-
While S
-
Turtle S
-
Script Mode S
-
Debugging S
- 2 Strings 7
-
Slice S
-
Raw Strings S
-
Regex A S
-
Validation S
-
Config S
-
Security S
-
Encrypt A S
- 3 Collections 6
-
Lists S
-
Dictionaries S
-
Efficiency S
-
Tree S
-
Tuples S
-
References S
- 4 Functions 5
-
Recursion S
-
Factorial S
-
Modulus S
-
Reassignment S
-
Approximate S
- 5 Storage 8
-
Files S
-
Databases S
-
Pipes S
-
With open S
-
Shelve A S
-
Zip S
-
Csv S
-
Json S
- 6 Class 4
-
Definition S
-
Attributes S
-
Functional S
-
Methods S
- 7 Goodies 5
-
Conditional Expression S
-
List Comprehension A S
-
Generator S
-
Named Tuple S
-
Modules S
- 8 Applications 5
-
Pythagora A S
-
Palindrome A S
-
Binary Search A S
-
Conway Game A S
-
Coin Flip A S
- 9 Scheduler 4
-
Time S
-
Multithreading A S
-
Subprocess S
-
Logging S
- 10 Packages 6
-
Clipboard A S
-
Ocr A S
-
Socket S
-
Image S
-
Virtualenv S
-
Jupyter S
S
R
Q
Python Strings Regex
import re pattern = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d') result = pattern.search('00111-222-444455') print(result.group()) #111-222-4444
Find Text
p162 Finding patterns in a normal way, without regular expressions.
"""Find text in phone format: 415-555-1234
Not using regex involves a lot of code.
If you want to find a phone within a larger text ...
you would have to add even more code.
"""
def is_phone_number(text):
if len(text) != 12:
return False
for i in range(0, 3):
if not text[i].isdecimal():
return False
if text[3] != '-':
return False
for i in range(4, 7):
if not text[i].isdecimal():
return False
if text[7] != '-':
return False
for i in range(8, 12):
if not text[i].isdecimal():
return False
return True
assert is_phone_number('123456789012') == False
assert is_phone_number('123-456-7777') == True
assert is_phone_number('123-4567777') == False
text = """Call me at 123-456-7777 or 415-555-1234,
but not at 415-5551234."""
for i in range(len(text)):
chunk = text[i:i+12]
if is_phone_number(chunk):
print("Found phone number: " + chunk)
print("Done")
# Found phone number: 123-456-7777
# Found phone number: 415-555-1234
# Done
Patterns
p164 Regular expressions are descriptions for a pattern of text.
"""Find text in phone format: 415-555-1234
Use regular expression patterns.
The group() method returns the match.
The findAll() method returns a list of strings.
"""
from nis import match
import re
# Check if string is phone number
def is_phone_number(text):
pattern = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
result = pattern.search(text)
if result == None:
return False
return True
assert is_phone_number('123456789012') == False
assert is_phone_number('123-456-7777') == True
assert is_phone_number('123-4567777') == False
# Search phone numbers in a text
text = 'Call me at 123-456-7777 or 415-555-1234, but not at 415-5551234.'
def search_first_number(text):
pattern = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
result = pattern.search(text)
return result.group()
def search_all_numbers(text):
pattern = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
result = pattern.findall(text)
return result
print("First number: " + search_first_number(text))
# First number: 123-456-7777
numbers = search_all_numbers(text) # list of strings
print("Numbers: \n" + '\n'.join(numbers))
# Numbers:
# 123-456-7777
# 415-555-1234
pattern = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
result = pattern.search('00111-222-444455')
print(result.group())
# 111-222-4444
Groups
p166 The groups() method returns a tuple of multiple values.
"""Regex: Grouping with parenthesses
Adding parenthesses will create groups in the regex.
"""
import re
text = 'My number is 415-555-1234'
pattern = re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)')
result = pattern.search(text)
code, number = result.groups()
assert code == '415'
assert number == '555-1234'
assert number != '415-555-1234'
Verbose
p178 You can use triple quote syntax to spread regex on multiple lines.
"""Regex on multiple lines
This verbose mode can be enabled with re.VERBOSE
"""
import re
pattern = re.compile(r'''
(\d{3}) # area code
(\s|-)? # separator
(
\d{3} # 3 digits
(\s|-) # separator
\d{4} # 4 digits
)
''', re.VERBOSE)
result = pattern.search('My number is 415 555-1234')
groups = result.groups()
code, sep, number, sep = groups
assert groups == ('415', ' ', '555-1234', '-')
assert code == '415'
assert number == '555-1234'
assert number != '415-555-1234'
➥ Applications (2)
Applications (2)
A program to find some pattern in a full page text.
"""Find authors App:
Find all authors @nickname in an amazon page
1) Open https://www.amazon.com/gp/product/1593279922
2) Copy Ctrl-A, Ctrl-C
3) Run python program
4) Paste Ctrl-v
"""
import re, pyperclip
clipboard = pyperclip.paste()
pattern = re.compile('@[a-zA-Z0-9_-]+')
authors = pattern.findall(clipboard)
pyperclip.copy('\n'.join(authors))
print(pyperclip.paste())
# @OscarBaruffa
# @Awful_Curious
# @mcapablanca
Use regex to find if a password is strong.

"""Strong password detection:
At least 8 character long, contains both uppercase and lowercase,
and has at least one digit,
and has at least one non-word character
"""
import re
def password_is_strong(password):
pattern = r'''(
(?=.*[a-z]+) # positive look ahead, at least one lowercase
(?=.*[A-Z]+) # positive look ahead, at least one upper case
(?=.*[\d]+) # positive look ahead, at least one digit
(?=.*[\W]+) # positive look ahead, at least one non-word
.{8,} # plus 5 more characters
)'''
pattern = re.compile(pattern, re.VERBOSE)
result = pattern.search(password)
return result != None
assert password_is_strong("abc") == False
assert password_is_strong("abcdefgh") == False
assert password_is_strong("Abcd2efgh!") == True
assert password_is_strong("aB2&bcde") == True
assert password_is_strong("aBcefg1!") == True
assert password_is_strong("aBcef1!") == False
➥ Questions