10.6. Regex Syntax Quantifier¶
Quantifier specifies how many occurrences of preceding qualifier or identifier
Exact
Greedy
Lazy
>>> import re
>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sat, Jan 1st, 2000 at 12:00 AM'
>>> re.findall(r'\d', TEXT)
['1', '2', '0', '0', '0', '1', '2', '0', '0']
>>> re.findall(r'\d\d\d\d', TEXT)
['2000']
10.6.1. Exact¶
Exact match
{n}
- exactly n repetitions
>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sat, Jan 1st, 2000 at 12:00 AM'
>>> re.findall(r'\d{1}', TEXT)
['1', '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)
[]
10.6.2. 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{0,1}
- minimum 0 repetitions, maximum 1 repetitions (maybe)*
- 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: Sat, Jan 1st, 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)
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '1', '', '', '', '', '2000', '',
'', '', '', '12', '', '00', '', '', '', '']
Note
Note, that zero (none) digits is a valid match for \d{,4}
.
Plus:
>>> re.findall(r'\d{1,}', TEXT)
['1', '2000', '12', '00']
>>>
>>> re.findall(r'\d+', TEXT)
['1', '2000', '12', '00']
Star:
>>> re.findall(r'\d{0,}', TEXT)
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '1', '', '', '', '', '2000', '',
'', '', '', '12', '', '00', '', '', '', '']
>>>
>>> re.findall(r'\d*', TEXT)
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '1', '', '', '', '', '2000', '',
'', '', '', '12', '', '00', '', '', '', '']
Question mark:
>>> re.findall(r'\d{0,1}', TEXT)
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '1', '', '', '', '', '2', '0',
'0', '0', '', '', '', '', '1', '2', '', '0', '0', '', '', '', '']
>>>
>>> re.findall(r'\d?', TEXT)
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '1', '', '', '', '', '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.
10.6.3. 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)*?
- 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: Sat, Jan 1st, 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)
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '1', '', '', '', '', '', '2',
'', '0', '', '0', '', '0', '', '', '', '', '', '1', '', '2', '', '', '0',
'', '0', '', '', '', '']
Plus:
>>> re.findall(r'\d{1,}?', TEXT)
['1', '2', '0', '0', '0', '1', '2', '0', '0']
>>>
>>> re.findall(r'\d+?', TEXT)
['1', '2', '0', '0', '0', '1', '2', '0', '0']
Star:
>>> re.findall(r'\d{0,}?', TEXT)
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '1', '', '', '', '', '', '2',
'', '0', '', '0', '', '0', '', '', '', '', '', '1', '', '2', '', '', '0',
'', '0', '', '', '', '']
>>>
>>> re.findall(r'\d*?', TEXT)
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '1', '', '', '', '', '', '2',
'', '0', '', '0', '', '0', '', '', '', '', '', '1', '', '2', '', '', '0',
'', '0', '', '', '', '']
Question mark:
>>> re.findall(r'\d{0,1}?', TEXT)
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '1', '', '', '', '', '', '2',
'', '0', '', '0', '', '0', '', '', '', '', '', '1', '', '2', '', '', '0',
'', '0', '', '', '', '']
>>>
>>> re.findall(r'\d??', TEXT)
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '', '', '', '1', '', '', '', '', '', '2',
'', '0', '', '0', '', '0', '', '', '', '', '', '1', '', '2', '', '', '0',
'', '0', '', '', '', '']
10.6.4. Greedy vs. Lazy¶
>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sat, Jan 1st, 2000 at 12:00 AM'
>>>
>>> re.findall('\d+', TEXT)
['1', '2000', '12', '00']
>>>
>>> re.findall('\d+?', TEXT)
['1', '2', '0', '0', '0', '1', '2', '0', '0']
>>> 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.']
10.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
10.6.6. Use Case - 0x01¶
Float
>>> TEXT = 'Pi number is 3.1415...'
>>>
>>> pi = re.findall(r'\d+\.\d+', TEXT)
>>> pi
['3.1415']
10.6.7. Use Case - 0x02¶
Time
>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sat, Jan 1st, 2000 at 12:00 AM'
>>>
>>> re.findall(r'\d\d?:\d\d', TEXT)
['12:00']
10.6.8. Use Case - 0x03¶
Date
>>> import re
>>> from datetime import datetime
>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sat, Jan 1st, 2000 at 12:00 AM'
>>>
>>> result = re.findall(r'\w{3} \d{1,2}st, \d{4}', TEXT)
>>>
>>> result
['Jan 1st, 2000']
10.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')]
10.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>']
10.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>']
10.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']
10.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>']
10.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>']
10.6.15. Assignments¶
"""
* Assignment: RE Syntax Quantifier
* Complexity: easy
* Lines of code: 2 lines
* Time: 2 min
English:
1. Define `result: str` with regular expression to find:
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. Zdefiniuj `result: str` z wyrażeniem regularnym aby wyszukać:
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ść
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)
>>> pprint(result, compact=True, width=72)
['1969', '1969']
>>> result = re.findall(result_b, TEXT)
>>> pprint(result, 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'
# Define only regex pattern (str), not re.findall(..., TEXT)
# type: str
result_a = ...
# 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(..., TEXT)
# type: str
result_b = ...
"""
* Assignment: RE Syntax Quantifier
* Complexity: easy
* Lines of code: 2 lines
* Time: 2 min
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ść
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)
>>> 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, TEXT)
>>> pprint(result, compact=True, width=72)
['47.5', '21.5']
"""
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 in text (as long as possible)
# Example: '11', '20', '1969', ...
# Note: define only regex pattern (str), not re.findall(...)
# type: str
result_a = ...
# Find all floats in text
# Example: '47.5', '21.5'
# Note: define only regex pattern (str), not re.findall(...)
# type: str
result_b = ...
"""
* Assignment: RE Syntax Quantifier
* Complexity: easy
* Lines of code: 3 lines
* Time: 2 min
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ść
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)
>>> 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, TEXT)
>>> pprint(result, compact=True, width=72)
['Neil Armstrong', 'Buzz Aldrin', 'Apollo Lunar', 'Michael Collins',
'Command Module']
>>> result = re.findall(result_c, TEXT)
>>> pprint(result, compact=True, width=72)
['Apollo 11', 'July 20', 'July 21']
"""
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 capitalized words
# Example: 'Apollo', 'Moon', 'Commander', 'Neil', 'Armstrong', ...
# Note: define only regex pattern (str), not re.findall(...)
# type: str
result_a = ...
# 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 = ...
# Find all names with numbers (capitalized word followed by number)
# Example: 'Apollo 11', 'July 20', 'July 21'
# Note: define only regex pattern (str), not re.findall(...)
# type: str
result_c = ...
"""
* Assignment: RE Syntax Quantifier
* Complexity: easy
* Lines of code: 3 lines
* Time: 2 min
English:
1. Define `result: str` with regular expression to find:
a. times (hours and minutes)
b. dates in US long format
c. durations in text
2. Run doctests - all must succeed
Polish:
1. Zdefiniuj `result: str` z wyrażeniem regularnym aby wyszukać:
a. czasy (godziny z minutami)
b. daty w formacie amerykańskim długim
c. 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
>>> from pprint import pprint
>>> result = re.findall(result_a, TEXT)
>>> pprint(result, compact=True, width=72)
['20:17', '02:56']
>>> result = re.findall(result_b, TEXT)
>>> pprint(result, compact=True, width=72)
['July 20, 1969', 'July 21, 1969']
>>> result = re.findall(result_c, TEXT)
>>> pprint(result, 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 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 = ...
# 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 = ...
# 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 = ...