9.6. Syntax Quantifier¶
Quantifier specifies how many occurrences of preceding qualifier or identifier
Exact
Greedy
Lazy
>>> import re
>>> TEXT = 'Mark Watney of Ares 3 landed on Mars on: Nov 7th, 2035 at 1:37 pm'
>>> re.findall(r'\d', TEXT)
['3', '7', '2', '0', '3', '5', '1', '3', '7']
>>> re.findall(r'\d\d\d\d', TEXT)
['2035']
9.6.1. Exact¶
Exact match
{n}
- exactly n repetitions
>>> TEXT = 'Mark Watney of Ares 3 landed on Mars on: Nov 7th, 2035 at 1:37 pm'
>>> re.findall(r'[0-9]{2}', TEXT)
['20', '35', '37']
>>> re.findall(r'\d{2}', TEXT)
['20', '35', '37']
9.6.2. Greedy¶
Prefer longest matches
Works better with numbers
Not that good results for text
Default behavior
{,n}
- maximum n repetitions, prefer longer{n,}
- minimum n repetitions, prefer longer{n,m}
- minimum n repetitions, maximum m times, prefer longer*
- 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 = 'Mark Watney of Ares 3 landed on Mars on: Nov 7th, 2035 at 1:37 pm'
>>> re.findall(r'\d{2,4}', TEXT)
['2035', '37']
9.6.3. Lazy¶
Prefer shortest matches
Works better with text
Not that good results for numbers
Non-greedy
{,n}?
- maximum n repetitions, prefer shorter{n,}?
- minimum n repetitions, prefer shorter{n,m}?
- minimum n repetitions, maximum m times, prefer shorter*?
- 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 = 'Mark Watney of Ares 3 landed on Mars on: Nov 7th, 2035 at 1:37 pm'
>>> re.findall(r'\d{2,4}?', TEXT)
['20', '35', '37']
9.6.4. Greedy vs. Lazy¶
>>> TEXT = 'Mark Watney of Ares 3 landed on Mars on: Nov 7th, 2035 at 1:37 pm'
>>>
>>> re.findall('\d+', TEXT)
['3', '7', '2035', '1', '37']
>>>
>>> re.findall('\d+?', TEXT)
['3', '7', '2', '0', '3', '5', '1', '3', '7']
>>> TEXT = 'Mark Watney is an astronaut. Ares 3 landed on Mars on: Nov 7th, 2035 at 13:37.'
>>>
>>> sentence = r'[A-Z].+\.'
>>> re.findall(sentence, TEXT)
['Mark Watney is an astronaut. Ares 3 landed on Mars on: Nov 7th, 2035 at 13:37.']
>>>
>>> sentence = r'[A-Z].+?\.'
>>> re.findall(sentence, TEXT)
['Mark Watney is an astronaut.', 'Ares 3 landed on Mars on: Nov 7th, 2035 at 13:37.']
9.6.5. 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
9.6.6. Use Case - 0x01¶
Float
>>> TEXT = 'Pi number is 3.1415...'
>>>
>>> pi = re.findall(r'\d+\.\d+', TEXT)
>>> pi
['3.1415']
9.6.7. Use Case - 0x02¶
Time
>>> TEXT = 'Mark Watney of Ares 3 landed on Mars on: Nov 7th, 2035 at 1:37 pm'
>>>
>>> re.findall(r'\d\d:\d\d', TEXT)
[]
>>>
>>> re.findall(r'\d\d?:\d\d', TEXT)
['1:37']
9.6.8. Use Case - 0x03¶
Date
>>> import re
>>> from datetime import datetime
>>> TEXT = 'Mark Watney of Ares 3 landed on Mars on: Nov 7th, 2035 at 1:37 pm'
>>>
>>> result = re.findall(r'\w{3} \d{1,2}th, \d{4}', TEXT)
>>>
>>> result
['Nov 7th, 2035']
9.6.9. Use Case - 0x04¶
>>> 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')]
9.6.10. Use Case - 0x05¶
>>> 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>']
9.6.11. Use Case - 0x06¶
>>> 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>']
9.6.12. Use Case - 0x07¶
>>> 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']
9.6.13. Use Case - 0x08¶
>>> 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>']
9.6.14. Use Case - 0x09¶
>>> 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>']
9.6.15. Assignments¶
"""
* Assignment: RE Syntax Quantifier
* Complexity: easy
* Lines of code: 2 lines
* Time: 3 min
English:
1. Use regular expressions find in text:
a. all years (four digits together)
b. all three letter acronyms (standalone word with three uppercase letters)
2. Run doctests - all must succeed
Polish:
1. Użyj wyrażeń regularnych wyszukiwania w tekście:
a. wszystkie lata (cztery cyfry razem)
b. wszystkie trzy literowe akronimy (słowo z trzech dużych liter)
2. Uruchom doctesty - wszystkie muszą się powieść
Hint:
* `re.findall()`
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
>>> pprint(result_a, compact=True, width=72)
['1969', '1969']
>>> pprint(result_b, compact=True, width=72)
['CDR', 'LMP', 'UTC', 'EVA', 'EVA', 'UTC', 'CMP']
"""
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 years (four digits together)
# Example: '1969', '1969'
# type: list[str]
result_a = ...
# Find all three letter acronyms (standalone word with three uppercase letters)
# Example: 'CDR', 'LMP', 'UTC', 'EVA', 'EVA', 'UTC', 'CMP'
# type: list[str]
result_b = ...
"""
* Assignment: RE Syntax Quantifier
* Complexity: easy
* Lines of code: 4 lines
* Time: 5 min
TODO: This assignment is not ready yet.
English:
1. Use regular expressions find in text
2. Run doctests - all must succeed
Polish:
1. Użyj wyrażeń regularnych wyszukiwania w tekście
2. Uruchom doctesty - wszystkie muszą się powieść
Hint:
* `re.findall()`
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
>>> pprint(result_a, 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']
>>> pprint(result_b, 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']
>>> pprint(result_c, compact=True, width=72)
['20:17', '02:56']
>>> pprint(result_d, compact=True, width=72)
['47.5', '21.5']
>>> pprint(result_e, 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']
>>> pprint(result_f, compact=True, width=72)
['Neil Armstrong', 'Buzz Aldrin', 'Apollo Lunar', 'Michael Collins',
'Command Module']
>>> pprint(result_g, compact=True, width=72)
['Apollo 11', 'July 20', 'July 21']
>>> pprint(result_h, compact=True, width=72)
[]
>>> pprint(result_i, compact=True, width=72)
['6 hours 39 minutes', '2 hours 31 minutes', '21 hours 36 minutes']
"""
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 integers (as long as possible)
# Example: '11', '20', '1969', ...
# type: list[str]
result_a = re.findall(r'[0-9]{1,}', TEXT)
# Find all integers in text (as long as possible)
# Example: '11', '20', '1969', ...
# type: list[str]
result_b = re.findall(r'[0-9]+', TEXT)
# Find all times in text
# Example: '20:17', '02:56'
# type: list[str]
result_c = re.findall(r'[0-9]+:[0-9]+', TEXT)
# Find all floats in text
# Example: '47.5', '21.5'
# type: list[str]
result_d = re.findall(r'[0-9]+\.[0-9]+', TEXT)
# Find all capitalized words
# Example: 'Apollo', 'Moon', 'Commander', 'Neil', 'Armstrong', ...
# type: list[str]
result_e = re.findall(r'[A-Z][a-z]+', TEXT)
# Find all names (two capitalized words) in text
# Example: 'Neil Armstrong', 'Buzz Aldrin', 'Apollo Lunar', 'Tranquility Base', ...
# type: list[str]
result_f = re.findall(r'[A-Z][a-z]+ [A-Z][a-z]+', TEXT)
# Find all names with numbers (capitalized word followed by number)
# Example: 'Apollo 11', 'July 20', 'July 21'
# type: list[str]
result_g = re.findall(r'[A-Z][a-z]+ [0-9]+', TEXT)
# Find all dates in US long format
# Example: 'July 20, 1969', 'July 21, 1969'
# type: list[str]
result_h = re.findall(r'[A-Z][a-z]+ [0-9]+, [0-9]+', TEXT)
# Find all durations in text
# Example: '6 hours 39 minutes', '2 hours 31 minutes', '21 hours 36 minutes'
# type: list[str]
result_i = re.findall(r'[0-9]+ hours [0-9]+ minutes', TEXT)
"""
* Assignment: RE Syntax Quantifier
* Complexity: easy
* Lines of code: 4 lines
* Time: 5 min
TODO: This assignment is not ready yet.
English:
1. Use regular expressions find in text
2. Run doctests - all must succeed
Polish:
1. Użyj wyrażeń regularnych wyszukiwania w tekście
2. Uruchom doctesty - wszystkie muszą się powieść
Hint:
* `re.findall()`
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
>>> pprint(result_a, compact=True, width=72)
['CDR', 'LMP', 'UTC', 'EVA', 'EVA', 'UTC', 'CMP']
>>> pprint(result_b, 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']
>>> pprint(result_c, compact=True, width=72)
['20:17', '02:56']
>>> pprint(result_d, compact=True, width=72)
['47.5', '21.5']
>>> pprint(result_e, 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']
>>> pprint(result_f, compact=True, width=72)
['Neil Armstrong', 'Buzz Aldrin', 'Apollo Lunar', 'Michael Collins',
'Command Module']
>>> pprint(result_g, compact=True, width=72)
['Apollo 11', 'July 20', 'July 21']
>>> pprint(result_h, compact=True, width=72)
[]
>>> pprint(result_i, compact=True, width=72)
['6 hours 39 minutes', '2 hours 31 minutes', '21 hours 36 minutes']
"""
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 three letter acronyms in text (standalone word with three capitalized letters)
# Example: 'CDR', 'LMP', 'UTC', 'EVA', 'EVA', 'UTC', 'CMP'
# type: list[str]
result_a = re.findall(r'[A-Z]{3}', TEXT)
# Find all integers in text (as long as possible)
# Example: '11', '20', '1969', ...
# type: list[str]
result_b = re.findall(r'[0-9]+', TEXT)
# Find all times in text
# Example: '20:17', '02:56'
# type: list[str]
result_c = re.findall(r'[0-9]+:[0-9]+', TEXT)
# Find all floats in text
# Example: '47.5', '21.5'
# type: list[str]
result_d = re.findall(r'[0-9]+\.[0-9]+', TEXT)
# Find all capitalized words
# Example: 'Apollo', 'Moon', 'Commander', 'Neil', 'Armstrong', ...
# type: list[str]
result_e = re.findall(r'[A-Z][a-z]+', TEXT)
# Find all names (two capitalized words) in text
# Example: 'Neil Armstrong', 'Buzz Aldrin', 'Apollo Lunar', 'Tranquility Base', ...
# type: list[str]
result_f = re.findall(r'[A-Z][a-z]+ [A-Z][a-z]+', TEXT)
# Find all names with numbers (capitalized word followed by number)
# Example: 'Apollo 11', 'July 20', 'July 21'
# type: list[str]
result_g = re.findall(r'[A-Z][a-z]+ [0-9]+', TEXT)
# Find all dates in US long format
# Example: 'July 20, 1969', 'July 21, 1969'
# type: list[str]
result_h = re.findall(r'[A-Z][a-z]+ [0-9]+, [0-9]+', TEXT)
# Find all durations in text
# Example: '6 hours 39 minutes', '2 hours 31 minutes', '21 hours 36 minutes'
# type: list[str]
result_i = re.findall(r'[0-9]+ hours [0-9]+ minutes', TEXT)
"""
* Assignment: RE Syntax Quantifier
* Complexity: easy
* Lines of code: 4 lines
* Time: 5 min
TODO: This assignment is not ready yet.
English:
1. Use regular expressions find in text
2. Run doctests - all must succeed
Polish:
1. Użyj wyrażeń regularnych wyszukiwania w tekście
2. Uruchom doctesty - wszystkie muszą się powieść
Hint:
* `re.findall()`
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
>>> pprint(result_a, compact=True, width=72)
['CDR', 'LMP', 'UTC', 'EVA', 'EVA', 'UTC', 'CMP']
>>> pprint(result_b, 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']
>>> pprint(result_c, compact=True, width=72)
['20:17', '02:56']
>>> pprint(result_d, compact=True, width=72)
['47.5', '21.5']
>>> pprint(result_e, 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']
>>> pprint(result_f, compact=True, width=72)
['Neil Armstrong', 'Buzz Aldrin', 'Apollo Lunar', 'Michael Collins',
'Command Module']
>>> pprint(result_g, compact=True, width=72)
['Apollo 11', 'July 20', 'July 21']
>>> pprint(result_h, compact=True, width=72)
[]
>>> pprint(result_i, compact=True, width=72)
['6 hours 39 minutes', '2 hours 31 minutes', '21 hours 36 minutes']
"""
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 three letter acronyms in text (standalone word with three capitalized letters)
# Example: 'CDR', 'LMP', 'UTC', 'EVA', 'EVA', 'UTC', 'CMP'
# type: list[str]
result_a = re.findall(r'[A-Z]{3}', TEXT)
# Find all integers in text (as long as possible)
# Example: '11', '20', '1969', ...
# type: list[str]
result_b = re.findall(r'[0-9]+', TEXT)
# Find all times in text
# Example: '20:17', '02:56'
# type: list[str]
result_c = re.findall(r'[0-9]+:[0-9]+', TEXT)
# Find all floats in text
# Example: '47.5', '21.5'
# type: list[str]
result_d = re.findall(r'[0-9]+\.[0-9]+', TEXT)
# Find all capitalized words
# Example: 'Apollo', 'Moon', 'Commander', 'Neil', 'Armstrong', ...
# type: list[str]
result_e = re.findall(r'[A-Z][a-z]+', TEXT)
# Find all names (two capitalized words) in text
# Example: 'Neil Armstrong', 'Buzz Aldrin', 'Apollo Lunar', 'Tranquility Base', ...
# type: list[str]
result_f = re.findall(r'[A-Z][a-z]+ [A-Z][a-z]+', TEXT)
# Find all names with numbers (capitalized word followed by number)
# Example: 'Apollo 11', 'July 20', 'July 21'
# type: list[str]
result_g = re.findall(r'[A-Z][a-z]+ [0-9]+', TEXT)
# Find all dates in US long format
# Example: 'July 20, 1969', 'July 21, 1969'
# type: list[str]
result_h = re.findall(r'[A-Z][a-z]+ [0-9]+, [0-9]+', TEXT)
# Find all durations in text
# Example: '6 hours 39 minutes', '2 hours 31 minutes', '21 hours 36 minutes'
# type: list[str]
result_i = re.findall(r'[0-9]+ hours [0-9]+ minutes', TEXT)