6.7. Regex Syntax Quantifier
Quantifier specifies how many occurrences of preceding qualifier or character class
Exact
Greedy
Lazy
6.7.1. SetUp
>>> import re
>>>
>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sun, Jan 2nd, 2000 at 12:00 AM'
6.7.2. Recap
>>> re.findall(r'\d', TEXT)
['2', '2', '0', '0', '0', '1', '2', '0', '0']
>>> re.findall(r'\d\d\d\d', TEXT)
['2000']
6.7.3. Exact
Exact match
{n}
- exactly n repetitions
>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sun, Jan 2nd, 2000 at 12:00 AM'
>>> re.findall(r'\d{1}', TEXT)
['2', '2', '0', '0', '0', '1', '2', '0', '0']
>>> re.findall(r'\d{2}', TEXT)
['20', '00', '12', '00']
>>> re.findall(r'\d{3}', TEXT)
['200']
>>> re.findall(r'\d{4}', TEXT)
['2000']
>>> re.findall(r'\d{5}', TEXT)
[]
6.7.4. Greedy
Prefer longest matches
Works better with numbers
Not that good results for text
Default behavior
{n,m}
- minimum n repetitions, maximum m times, prefer longer{,n}
- maximum n repetitions, prefer longer{n,}
- minimum n repetitions, prefer longer
>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sun, Jan 2nd, 2000 at 12:00 AM'
Min/max:
>>> re.findall(r'\d{2,4}', TEXT)
['2000', '12', '00']
Nolimit:
>>> re.findall(r'\d{2,}', TEXT)
['2000', '12', '00']
>>>
>>> re.findall(r'\d{,4}', TEXT)
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'2', '', '', '', '', '2000', '', '', '', '', '12', '', '00', '',
'', '', '']
Note
Note, that zero (none) digits is a valid match for \d{,4}
.
6.7.5. Lazy
Prefer shortest matches
Works better with text
Not that good results for numbers
Non-greedy
{n,m}?
- minimum n repetitions, maximum m times, prefer shorter{,n}?
- maximum n repetitions, prefer shorter{n,}?
- minimum n repetitions, prefer shorter{0,1}?
- minimum 0 repetitions, maximum 1 repetitions (maybe)
>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sun, Jan 2nd, 2000 at 12:00 AM'
Min/max:
>>> re.findall(r'\d{2,4}?', TEXT)
['20', '00', '12', '00']
Nolimit:
>>> re.findall(r'\d{2,}?', TEXT)
['20', '00', '12', '00']
>>>
>>> re.findall(r'\d{,4}?', TEXT)
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '2', '', '', '', '', '', '2', '', '0', '', '0', '', '0', '',
'', '', '', '', '1', '', '2', '', '', '0', '', '0', '', '', '', '']
6.7.6. Greedy vs. Lazy
r'\d+'
- Greedyr'\d+?'
- Lazy
>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sun, Jan 2nd, 2000 at 12:00 AM'
>>> re.findall(r'\d+', TEXT)
['2', '2000', '12', '00']
>>>
>>> re.findall(r'\d+?', TEXT)
['2', '2', '0', '0', '0', '1', '2', '0', '0']
>>> TEXT = 'Email from Mark Watney. Received on Sunday.'
>>>
>>> sentence = r'[A-Z].+\.'
>>> re.findall(sentence, TEXT)
['Email from Mark Watney. Received on Sunday.']
>>>
>>> sentence = r'[A-Z].+?\.'
>>> re.findall(sentence, TEXT)
['Email from Mark Watney.', 'Received on Sunday.']
Mind the number of sentences in each case. Without lazy quantifier it returns only one result: from first capital letter to the last possible dot. Lazy quantifier splits text into two parts. From the first capital letter to the closest dot.
6.7.7. Shorthand Greedy
*
- minimum 0 repetitions, no maximum, prefer longer (alias to{0,}
)+
- minimum 1 repetitions, no maximum, prefer longer (alias to{1,}
)?
- minimum 0 repetitions, maximum 1 repetitions, prefer longer (alias to{0,1}
)
>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sun, Jan 2nd, 2000 at 12:00 AM'
Plus:
>>> re.findall(r'\d{1,}', TEXT)
['2', '2000', '12', '00']
>>>
>>> re.findall(r'\d+', TEXT)
['2', '2000', '12', '00']
Star:
>>> re.findall(r'\d{0,}', TEXT)
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'2', '', '', '', '', '2000', '', '', '', '', '12', '', '00', '',
'', '', '']
>>>
>>> re.findall(r'\d*', TEXT)
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'2', '', '', '', '', '2000', '', '', '', '', '12', '', '00', '',
'', '', '']
Question mark:
>>> re.findall(r'\d{0,1}', TEXT)
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'2', '', '', '', '', '2', '0', '0', '0', '', '', '', '', '1', '2',
'', '0', '0', '', '', '', '']
>>>
>>> re.findall(r'\d?', TEXT)
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'2', '', '', '', '', '2', '0', '0', '0', '', '', '', '', '1', '2',
'', '0', '0', '', '', '', '']
Note
Both star and question mark does not make any sense with numbers. They works better with text.
6.7.8. Shorthand Lazy
*?
- minimum 0 repetitions, no maximum, prefer shorter (alias to{0,}?
)+?
- minimum 1 repetitions, no maximum, prefer shorter (alias to{1,}?
)??
- minimum 0 repetitions, maximum 1 repetition, prefer shorter (alias to{0,1}?
)
>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sun, Jan 2nd, 2000 at 12:00 AM'
>>> re.findall(r'\d{1,}?', TEXT)
['2', '2', '0', '0', '0', '1', '2', '0', '0']
>>>
>>> re.findall(r'\d+?', TEXT)
['2', '2', '0', '0', '0', '1', '2', '0', '0']
Star:
>>> re.findall(r'\d{0,}?', TEXT)
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '2', '', '', '', '', '', '2', '', '0', '', '0', '', '0', '',
'', '', '', '', '1', '', '2', '', '', '0', '', '0', '', '', '', '']
>>>
>>> re.findall(r'\d*?', TEXT)
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '2', '', '', '', '', '', '2', '', '0', '', '0', '', '0', '',
'', '', '', '', '1', '', '2', '', '', '0', '', '0', '', '', '', '']
Question mark:
>>> re.findall(r'\d{0,1}?', TEXT)
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '2', '', '', '', '', '', '2', '', '0', '', '0', '', '0', '',
'', '', '', '', '1', '', '2', '', '', '0', '', '0', '', '', '', '']
>>>
>>> re.findall(r'\d??', TEXT)
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '2', '', '', '', '', '', '2', '', '0', '', '0', '', '0', '',
'', '', '', '', '1', '', '2', '', '', '0', '', '0', '', '', '', '']
6.7.9. Case Study 1
import re
html = '<p>Hello World</p>'
re.findall(r'<p>', html)
# ['<p>']
re.findall(r'</p>', html)
# ['</p>']
re.findall(r'</{0,1}p>', html)
# ['<p>', '</p>']
re.findall(r'</?p>', html)
# ['<p>', '</p>']
6.7.10. Case Study 2
import re
CODE = """
name = "Mark"
print(name)
"""
# %%
variable = re.findall(r'^(\w{1,}) =', CODE, flags=re.MULTILINE)
print(variable)
# %%
variable = re.findall(r'^(\w+) =', CODE, flags=re.MULTILINE)
print(variable)
import re
CODE = """
name ="Mark"
print(name)
"""
# %%
variable = re.findall(r'^(\w{1,})\s{0,1}=\s{0,1}', CODE, flags=re.MULTILINE)
print(variable)
# %%
variable = re.findall(r'^(\w+)\s?=\s?', CODE, flags=re.MULTILINE)
print(variable)
import re
CODE = """
name ="Mark"
print(name)
"""
# %%
variable = re.findall(r'^(\w{1,})\s{0,1}=\s{0,1}".{0,}"', CODE, flags=re.MULTILINE)
print(variable)
# %%
variable = re.findall(r'^(\w+)\s?=\s?".*"', CODE, flags=re.MULTILINE)
print(variable)
# name = ""
# name = "a"
# name = "abc"
import re
CODE = """
name = "Mark"
print(name)
"""
# %%
variable = re.findall(r'^(\w{1,})\s{0,}=\s{0,}".{0,}"', CODE, flags=re.MULTILINE)
print(variable)
# %%
variable = re.findall(r'^(\w+)\s*=\s*".*"', CODE, flags=re.MULTILINE)
print(variable)
# name = ""
# name = "a"
# name = "abc"
6.7.11. Case Study 3
import re
html = '<p>Hello World</p>'
# %%
re.findall(r'<.+>', html)
# ['<p>Hello World</p>']
# %%
re.findall(r'<.+?>', html)
# ['<p>', '</p>']
6.7.12. Case Study 4
import re
from pprint import pprint
text = 'Litwo. Ojczyzno moja. Ty jesteś jak zdrowie. Ile cię trzeba cenić, ten tylko się dowie, kto cię stracił. [bla, bla, bla] I ja tam z gośćmi byłem, miód i wino piłem. A com wiedział i słyszał, w księgi umieściłem.'
# %% Greedy
zdania = re.findall(r'[A-Z].+\.', text) # do najdalszej kropki
len(zdania)
# 1
print(zdania)
# ['Litwo. Ojczyzno moja. Ty jesteś jak zdrowie. Ile cię trzeba cenić, ten tylko się dowie, kto cię stracił. [bla, bla, bla] I ja tam z gośćmi byłem, miód i wino piłem. A com wiedział i słyszał, w księgi umieściłem.']
# %% Lazy
zdania = re.findall(r'[A-Z].+?\.', text) # do najbliższej kropki
len(zdania)
# 6
pprint(zdania)
# ['Litwo.',
# 'Ojczyzno moja.',
# 'Ty jesteś jak zdrowie.',
# 'Ile cię trzeba cenić, ten tylko się dowie, kto cię stracił.',
# 'I ja tam z gośćmi byłem, miód i wino piłem.',
# 'A com wiedział i słyszał, w księgi umieściłem.']
6.7.13. Examples
[0-9]{2}
- exactly two digits from 0 to 9\d{2}
- exactly two digits from 0 to 9[A-Z]{2,10}
- two to ten uppercase letters from A to Z[A-Z]{2-10}-[0-9]{,5}
- two to ten uppercase letters from A to Z followed by dash (-) and at least five numbers[a-z]+
- at least one lowercase letter from a to z, but try to fit the longest match\d+
- number\d+\.\d+
- float
6.7.14. Use Case - 1
Float
>>> TEXT = 'Pi number is 3.1415...'
>>>
>>> pi = re.findall(r'\d+\.\d+', TEXT)
>>> pi
['3.1415']
6.7.15. Use Case - 2
Time
>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sun, Jan 2nd, 2000 at 12:00 AM'
>>>
>>> re.findall(r'\d\d?:\d\d', TEXT)
['12:00']
6.7.16. Use Case - 3
Date
>>> import re
>>> from datetime import datetime
>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sun, Jan 2nd, 2000 at 12:00 AM'
>>>
>>> result = re.findall(r'\w{3} \d{1,2}nd, \d{4}', TEXT)
>>>
>>> result
['Jan 2nd, 2000']
6.7.17. Use Case - 4
>>> import re
>>> line = 'value=123'
>>>
>>> re.findall(r'(\w+)\s?=\s?(\d+)', line)
[('value', '123')]
>>> line = 'value = 123'
>>>
>>> re.findall(r'(\w+)\s?=\s?(\d+)', line)
[('value', '123')]
6.7.18. Use Case - 5
>>> import re
>>> HTML = '<h1>Header 1</h1><p>Paragraph 1</p><p>Paragraph 2</p>'
>>> re.findall(r'<p>.*</p>', HTML)
['<p>Paragraph 1</p><p>Paragraph 2</p>']
>>> re.findall(r'<p>.*?</p>', HTML)
['<p>Paragraph 1</p>', '<p>Paragraph 2</p>']
6.7.19. Use Case - 6
>>> import re
>>> HTML = '<h1>Header 1</h1><p>Paragraph 1</p><p>Paragraph 2</p>'
>>> re.findall(r'<p>', HTML)
['<p>', '<p>']
>>> re.findall(r'</p>', HTML)
['</p>', '</p>']
>>> re.findall(r'</?p>', HTML)
['<p>', '</p>', '<p>', '</p>']
6.7.20. Use Case - 7
>>> import re
>>> HTML = '<h1>Header 1</h1><p>Paragraph 1</p><p>Paragraph 2</p>'
>>> re.findall(r'<.+>', HTML)
['<h1>Header 1</h1><p>Paragraph 1</p><p>Paragraph 2</p>']
>>> re.findall(r'<.+?>', HTML)
['<h1>', '</h1>', '<p>', '</p>', '<p>', '</p>']
>>> re.findall(r'</?.+?>', HTML)
['<h1>', '</h1>', '<p>', '</p>', '<p>', '</p>']
>>> re.findall(r'</?(.+?)>', HTML)
['h1', 'h1', 'p', 'p', 'p', 'p']
>>> tags = re.findall(r'</?(.+?)>', HTML)
>>> sorted(set(tags))
['h1', 'p']
6.7.21. Use Case - 8
>>> import re
>>> HTML = '<h1>Header 1</h1><p>Paragraph 1</p><p>Paragraph 2</p>'
>>> re.findall(r'</?.*>', HTML)
['<h1>Header 1</h1><p>Paragraph 1</p><p>Paragraph 2</p>']
>>> re.findall(r'</?.*?>', HTML)
['<h1>', '</h1>', '<p>', '</p>', '<p>', '</p>']
6.7.22. Use Case - 9
>>> HTML = '<p>We choose to go to the Moon</p>'
>>>
>>> tag = r'<.+>'
>>> re.findall(tag, HTML)
['<p>We choose to go to the Moon</p>']
>>>
>>> tag = r'<.+?>'
>>> re.findall(tag, HTML)
['<p>', '</p>']
6.7.23. Assignments
# %% 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
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: RE Syntax Quantifier
# - Difficulty: easy
# - Lines: 2
# - Minutes: 2
# %% English
# 1. Define `result: str` with regular expression to find:
# - all years (four digits together)
# - all three letter acronyms (standalone word with three uppercase letters)
# 2. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: str` z wyrażeniem regularnym aby wyszukać:
# - wszystkie lata (cztery cyfry razem)
# - wszystkie trzy literowe akronimy (słowo z trzech dużych liter)
# 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
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from pprint import pprint
>>> result = re.findall(result_a, DATA)
>>> pprint(result, compact=True, width=72)
['1969', '1969']
>>> result = re.findall(result_b, DATA)
>>> pprint(result, compact=True, width=72)
['CDR', 'LMP', 'UTC', 'EVA', 'EVA', 'UTC', 'CMP']
"""
import re
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."""
# Find all years (four digits together)
# Example: '1969', '1969'
# Define only regex pattern (str), not re.findall(..., DATA)
# type: str
result_a = r''
# Find all three letter acronyms (standalone word with three uppercase letters)
# Example: 'CDR', 'LMP', 'UTC', 'EVA', 'EVA', 'UTC', 'CMP'
# Define only regex pattern (str), not re.findall(..., DATA)
# type: str
result_b = r''
# %% 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
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: RE Syntax Quantifier
# - Difficulty: easy
# - Lines: 2
# - Minutes: 2
# %% English
# 1. Use regular expressions find in text:
# - all integers (as long as possible)
# - all floats
# 2. Run doctests - all must succeed
# %% Polish
# 1. Użyj wyrażeń regularnych wyszukiwania w tekście:
# - wszystkie liczby całkowite (jak najdłuższe)
# - wszystkie liczby z ułamkami dziesiętnymi
# 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
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from pprint import pprint
>>> result = re.findall(result_a, DATA)
>>> pprint(result, compact=True, width=72)
['11', '20', '1969', '20', '17', '6', '39', '21', '1969', '02', '56',
'15', '19', '2', '31', '47', '5', '21', '5', '21', '36']
>>> result = re.findall(result_b, DATA)
>>> pprint(result, compact=True, width=72)
['47.5', '21.5']
"""
import re
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."""
# Find all integers in text (as long as possible)
# Example: '11', '20', '1969', ...
# Note: define only regex pattern (str), not re.findall(...)
# type: str
result_a = r''
# Find all floats in text
# Example: '47.5', '21.5'
# Note: define only regex pattern (str), not re.findall(...)
# type: str
result_b = r''
# %% 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
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: RE Syntax Quantifier
# - Difficulty: easy
# - Lines: 3
# - Minutes: 2
# %% English
# 1. Use regular expressions find in text
# - all capitalized words
# - all names (two capitalized words)
# - all names with numbers (capitalized word, space and number)
# 2. Run doctests - all must succeed
# %% Polish
# 1. Użyj wyrażeń regularnych wyszukiwania w tekście:
# - słowa zaczynające się wielką literą
# - wszystkie nazwy (dwa słowa zaczynające się wielką literą)
# - wszystkie nazwy z numerami (słowo z dużej litery, spacja i liczba)
# 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
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from pprint import pprint
>>> result = re.findall(result_a, DATA)
>>> pprint(result, compact=True, width=72)
['Apollo', 'American', 'Moon', 'Commander', 'Neil', 'Armstrong', 'Buzz',
'Aldrin', 'Apollo', 'Lunar', 'Module', 'Eagle', 'July', 'Armstrong',
'Moon', 'July', 'Aldrin', 'They', 'Tranquility', 'Base', 'Armstrong',
'Aldrin', 'Earth', 'Michael', 'Collins', 'Command', 'Module',
'Columbia', 'Moon', 'Columbia']
>>> result = re.findall(result_b, DATA)
>>> pprint(result, compact=True, width=72)
['Neil Armstrong', 'Buzz Aldrin', 'Apollo Lunar', 'Tranquility Base',
'Michael Collins', 'Command Module']
>>> result = re.findall(result_c, DATA)
>>> pprint(result, compact=True, width=72)
['Apollo 11']
"""
import re
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."""
# Find all capitalized words
# Example: 'Apollo', 'Moon', 'Commander', 'Neil', 'Armstrong', ...
# Note: define only regex pattern (str), not re.findall(...)
# type: str
result_a = r''
# Find all names (two capitalized words) in text
# Example: 'Neil Armstrong', 'Buzz Aldrin', 'Apollo Lunar', 'Tranquility Base', ...
# Note: define only regex pattern (str), not re.findall(...)
# type: str
result_b = r''
# Find all names with numbers (capitalized word followed by number)
# Example: 'Apollo 11'
# Note: define only regex pattern (str), not re.findall(...)
# type: str
result_c = r''
# %% 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
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: RE Syntax Quantifier
# - Difficulty: easy
# - Lines: 3
# - Minutes: 2
# %% English
# 1. Define `result: str` with regular expression to find:
# - times (hours and minutes)
# - dates in US long format
# - durations in text
# 2. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: str` z wyrażeniem regularnym aby wyszukać:
# - czasy (godziny z minutami)
# - daty w formacie amerykańskim długim
# - okresy
# 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
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from pprint import pprint
>>> result = re.findall(result_a, DATA)
>>> pprint(result, compact=True, width=72)
['20:17', '02:56']
>>> result = re.findall(result_b, DATA)
>>> pprint(result, compact=True, width=72)
['July 20, 1969', 'July 21, 1969']
>>> result = re.findall(result_c, DATA)
>>> pprint(result, compact=True, width=72)
['6 hours 39 minutes', '2 hours 31 minutes', '21 hours 36 minutes']
"""
import re
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 20, 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 21, 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 times in text
# Example: '20:17', '02:56'
# Note: define only regex pattern (str), not re.findall(...)
# type: str
result_a = r''
# Find all dates in US long format
# Example: 'July 20, 1969', 'July 21, 1969'
# Note: define only regex pattern (str), not re.findall(...)
# type: str
result_b = r''
# Find all durations in text
# Example: '6 hours 39 minutes', '2 hours 31 minutes', '21 hours 36 minutes'
# Note: define only regex pattern (str), not re.findall(...)
# type: str
result_c = r''