6.5. Regex Syntax Negation
Negation logically inverts qualifier
[^...]
- anything but ...
6.5.1. SetUp
>>> import re
6.5.2. Syntax
[^...]
- anything but ...
6.5.3. Example
[^abc]
- anything but letter a or b or c
>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sun, Jan 2nd, 2000 at 12:00 AM'
>>> re.findall(r'[0-9]', TEXT)
['2', '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', 'u', 'n', ',', ' ', 'J', 'a', 'n', ' ', 'n',
'd', ',', ' ', ' ', 'a', 't', ' ', ':', ' ', 'A', 'M']
6.5.4. Compare
>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sun, Jan 2nd, 2000 at 12:00 AM'
>>> re.findall(r'[0-9]', TEXT)
['2', '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', 'u', 'n', ',', ' ', 'J', 'a', 'n', ' ', 'n',
'd', ',', ' ', ' ', 'a', 't', ' ', ':', ' ', 'A', 'M']
>>> re.findall(r'^[^0-9]', TEXT)
['E']
6.5.5. Assignments
# %% About
# - Name: RE Syntax Negation
# - Difficulty: easy
# - Lines: 3
# - Minutes: 2
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% English
# 1. Define `result: str` with regular expression to find:
# - non-digits, example: ['\n', ' ', "'", '(', ')', ',', '.', ':']
# - non-lowercase-letters, example: ['\n', ' ', "'", '(', ')', ',', '.', '0', '1', '2', '3', ...]
# - non-uppercase-letters, example: ['\n', ' ', "'", '(', ')', ',', '.', '0', '1', '2', '3', ...]
# 2. Define only regex pattern (str), not re.findall(...)
# 3. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: str` z wyrażeniem regularnym aby wyszukać:
# - nie-cyfry, przykład: ['\n', ' ', "'", '(', ')', ',', '.', ':']
# - nie-małe-litery, przykład: ['\n', ' ', "'", '(', ')', ',', '.', '0', '1', '2', '3', ...]
# - nie-duże-litery, przykład: ['\n', ' ', "'", '(', ')', ',', '.', '0', '1', '2', '3', ...]
# 2. Zdefiniuj tylko wzorzec regex (str), nie re.findall(...)
# 3. 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
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from pprint import pprint
>>> result = re.findall(result_a, DATA)
>>> 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, DATA)
>>> 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, DATA)
>>> 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']
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% Imports
import re
# %% Types
result_a: str
result_b: str
result_c: str
# %% Data
DATA = """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."""
# %% Result
result_a = r''
result_b = r''
result_c = r''
# %% About
# - Name: RE Syntax Negation
# - Difficulty: easy
# - Lines: 2
# - Minutes: 2
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% English
# 1. Define `result: str` with regular expression to find:
# - non-letters (any case), example: ['\n', ' ', "'", '(', ')', ',', '.', ':']
# - non-digits and non-letters, example: ['\n', ' ', "'", '(', ')', ',', '.', ':']
# 2. Define only regex pattern (str), not re.findall(...)
# 3. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: str` z wyrażeniem regularnym aby wyszukać:
# - nie-litery (nie-małe i nie-duże), przykład: ['\n', ' ', "'", '(', ')', ',', '.', ':']
# - nie-cyfry i nie-litery, przykład: ['\n', ' ', "'", '(', ')', ',', '.', ':']
# 2. Zdefiniuj tylko wzorzec regex (str), nie re.findall(...)
# 3. 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
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from pprint import pprint
>>> result = re.findall(result_a, DATA)
>>> 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, DATA)
>>> result = sorted(set(result))
>>> pprint(result, compact=True, width=72)
['\\n', ' ', "'", '(', ')', ',', '.', ':']
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% Imports
import re
# %% Types
result_a: str
result_b: str
# %% Data
DATA = """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."""
# %% Result
result_a = r''
result_b = r''