10.4. Regex Syntax Negation¶
Negation logically inverts qualifier
[^...]
- anything but ...
10.4.1. SetUp¶
>>> import re
10.4.2. Syntax¶
[^...]
- anything but ...
10.4.3. Example¶
[^abc]
- anything but letter a or b or c
>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sat, Jan 1st, 2000 at 12:00 AM'
>>> re.findall(r'[0-9]', TEXT)
['1', '2', '0', '0', '0', '1', '2', '0', '0']
>>> re.findall(r'[^0-9]', TEXT)
['E', 'm', 'a', 'i', 'l', ' ', 'f', 'r', 'o', 'm', ' ', 'M', 'a', 'r', 'k',
' ', 'W', 'a', 't', 'n', 'e', 'y', ' ', '<', 'm', 'w', 'a', 't', 'n', 'e',
'y', '@', 'n', 'a', 's', 'a', '.', 'g', 'o', 'v', '>', ' ', 'r', 'e', 'c',
'e', 'i', 'v', 'e', 'd', ' ', 'o', 'n', ':', ' ', 'S', 'a', 't', ',', ' ',
'J', 'a', 'n', ' ', 's', 't', ',', ' ', ' ', 'a', 't', ' ', ':', ' ', 'A',
'M']
10.4.4. Compare¶
>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sat, Jan 1st, 2000 at 12:00 AM'
>>> re.findall(r'[0-9]', TEXT)
['1', '2', '0', '0', '0', '1', '2', '0', '0']
>>> re.findall(r'^[0-9]', TEXT)
[]
>>> re.findall(r'[^0-9]', TEXT)
['E', 'm', 'a', 'i', 'l', ' ', 'f', 'r', 'o', 'm', ' ', 'M', 'a', 'r', 'k',
' ', 'W', 'a', 't', 'n', 'e', 'y', ' ', '<', 'm', 'w', 'a', 't', 'n', 'e',
'y', '@', 'n', 'a', 's', 'a', '.', 'g', 'o', 'v', '>', ' ', 'r', 'e', 'c',
'e', 'i', 'v', 'e', 'd', ' ', 'o', 'n', ':', ' ', 'S', 'a', 't', ',', ' ',
'J', 'a', 'n', ' ', 's', 't', ',', ' ', ' ', 'a', 't', ' ', ':', ' ', 'A',
'M']
>>> re.findall(r'^[^0-9]', TEXT)
['E']
10.4.5. Assignments¶
"""
* Assignment: RE Syntax Negation
* Complexity: easy
* Lines of code: 3 lines
* Time: 2 min
English:
1. Define `result: str` with regular expression to find:
a. non-digits
b. non-lowercase-letters
c. non-uppercase-letters
2. Run doctests - all must succeed
Polish:
1. Zdefiniuj `result: str` z wyrażeniem regularnym aby wyszukać:
a. nie-cyfry
b. nie-małe-litery
c. nie-duże-litery
2. Uruchom doctesty - wszystkie muszą się powieść
References:
[1] Authors: Wikipedia contributors
Title: Apollo 11
Publisher: Wikipedia
Year: 2019
Retrieved: 2019-12-14
URL: https://en.wikipedia.org/wiki/Apollo_11
Tests:
>>> import sys; sys.tracebacklimit = 0
>>> from pprint import pprint
>>> result = re.findall(result_a, TEXT)
>>> result = sorted(set(result))
>>> pprint(result, compact=True, width=72)
['\\n', ' ', "'", '(', ')', ',', '.', ':', 'A', 'B', 'C', 'D', 'E', 'J',
'L', 'M', 'N', 'P', 'R', 'T', 'U', 'V', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'w', 'x', 'y', 'z']
>>> result = re.findall(result_b, TEXT)
>>> result = sorted(set(result))
>>> pprint(result, compact=True, width=72)
['\\n', ' ', "'", '(', ')', ',', '.', '0', '1', '2', '3', '4', '5', '6',
'7', '9', ':', 'A', 'B', 'C', 'D', 'E', 'J', 'L', 'M', 'N', 'P', 'R',
'T', 'U', 'V']
>>> result = re.findall(result_c, TEXT)
>>> result = sorted(set(result))
>>> pprint(result, compact=True, width=72)
['\\n', ' ', "'", '(', ')', ',', '.', '0', '1', '2', '3', '4', '5', '6',
'7', '9', ':', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'w', 'x', 'y', 'z']
"""
import re
TEXT = """Apollo 11 was the American spaceflight that first landed
humans on the Moon. Commander (CDR) Neil Armstrong and lunar module
pilot (LMP) Buzz Aldrin landed the Apollo Lunar Module (LM) Eagle on
July 20th, 1969 at 20:17 UTC, and Armstrong became the first person
to step (EVA) onto the Moon's surface (EVA) 6 hours 39 minutes later,
on July 21st, 1969 at 02:56:15 UTC. Aldrin joined him 19 minutes later.
They spent 2 hours 31 minutes exploring the site they had named Tranquility
Base upon landing. Armstrong and Aldrin collected 47.5 pounds (21.5 kg)
of lunar material to bring back to Earth as pilot Michael Collins (CMP)
flew the Command Module (CM) Columbia in lunar orbit, and were on the
Moon's surface for 21 hours 36 minutes before lifting off to rejoin
Columbia."""
# Find all non-digits
# Example: '\n', ' ', "'", '(', ')', ',', '.', ':'
# Note: define only regex pattern (str), not re.findall(...)
# type: str
result_a = ...
# Find all non-lowercase-letters
# Example: '\n', ' ', "'", '(', ')', ',', '.', '0', '1', '2', '3', ...
# Note: define only regex pattern (str), not re.findall(...)
# type: str
result_b = ...
# Find all non-uppercase-letters
# Example: '\n', ' ', "'", '(', ')', ',', '.', '0', '1', '2', '3', ...
# Note: define only regex pattern (str), not re.findall(...)
# type: str
result_c = ...
"""
* Assignment: RE Syntax Negation
* Complexity: easy
* Lines of code: 2 lines
* Time: 2 min
English:
1. Define `result: str` with regular expression to find:
a. non-letters (any case)
b. non-digits and non-letters
2. Run doctests - all must succeed
Polish:
1. Zdefiniuj `result: str` z wyrażeniem regularnym aby wyszukać:
a. nie-litery (nie-małe i nie-duże)
b. nie-cyfry i nie-litery
2. Uruchom doctesty - wszystkie muszą się powieść
References:
[1] Authors: Wikipedia contributors
Title: Apollo 11
Publisher: Wikipedia
Year: 2019
Retrieved: 2019-12-14
URL: https://en.wikipedia.org/wiki/Apollo_11
Tests:
>>> import sys; sys.tracebacklimit = 0
>>> from pprint import pprint
>>> result = re.findall(result_a, TEXT)
>>> result = sorted(set(result))
>>> pprint(result, compact=True, width=72)
['\\n', ' ', "'", '(', ')', ',', '.', '0', '1', '2', '3', '4', '5', '6',
'7', '9', ':']
>>> result = re.findall(result_b, TEXT)
>>> result = sorted(set(result))
>>> pprint(result, compact=True, width=72)
['\\n', ' ', "'", '(', ')', ',', '.', ':']
"""
import re
TEXT = """Apollo 11 was the American spaceflight that first landed
humans on the Moon. Commander (CDR) Neil Armstrong and lunar module
pilot (LMP) Buzz Aldrin landed the Apollo Lunar Module (LM) Eagle on
July 20th, 1969 at 20:17 UTC, and Armstrong became the first person
to step (EVA) onto the Moon's surface (EVA) 6 hours 39 minutes later,
on July 21st, 1969 at 02:56:15 UTC. Aldrin joined him 19 minutes later.
They spent 2 hours 31 minutes exploring the site they had named Tranquility
Base upon landing. Armstrong and Aldrin collected 47.5 pounds (21.5 kg)
of lunar material to bring back to Earth as pilot Michael Collins (CMP)
flew the Command Module (CM) Columbia in lunar orbit, and were on the
Moon's surface for 21 hours 36 minutes before lifting off to rejoin
Columbia."""
# Find all non-letters
# Example: '\n', ' ', "'", '(', ')', ',', '.', ':'
# Note: define only regex pattern (str), not re.findall(...)
# type: str
result_a = ...
# Find all non-digits and non-letters
# Example: '\n', ' ', "'", '(', ')', ',', '.', ':'
# Note: define only regex pattern (str), not re.findall(...)
# type: str
result_b = ...