9.5. Syntax Quantifier

9.5.1. Rationale

Quantifier specifies how many occurrences of preceding qualifier or identifier.

9.5.2. Exact

  • Exact match

  • {n} - exactly n repetitions, prefer longer

>>> import re
>>>
>>>
>>> TEXT = 'Yuri Gagarin launched to space on Apr 12th, 1961 at 6:07 am.'
>>>
>>> re.findall(r'[0-9]{2}', TEXT)
['12', '19', '61', '07']
>>>
>>> re.findall(r'\d{2}', TEXT)
['12', '19', '61', '07']

9.5.3. Greedy

  • Prefer longest matches

  • 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})

>>> import re
>>>
>>>
>>> TEXT = 'Yuri Gagarin launched to space on Apr 12th, 1961 at 6:07 am.'
>>>
>>> re.findall(r'\d{2,4}', TEXT)
['12', '1961', '07']

9.5.4. Lazy

  • Prefer shortest matches

  • 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}?)

>>> import re
>>>
>>>
>>> TEXT = 'Yuri Gagarin launched to space on Apr 12th, 1961 at 6:07 am.'
>>>
>>> re.findall(r'\d{2,4}?', TEXT)
['12', '19', '61', '07']

9.5.5. Greedy vs. Lazy

>>> import re
>>>
>>>
>>> TEXT = 'Yuri Gagarin launched to space on Apr 12th, 1961 at 6:07 am.'
>>>
>>> re.findall(r'\d{2,4}', TEXT)  # Greedy
['12', '1961', '07']
>>>
>>> re.findall(r'\d{2,4}?', TEXT)  # Lazy
['12', '19', '61', '07']

Greedy vs Lazy in exact match:

>>> import re
>>>
>>>
>>> TEXT = 'Yuri Gagarin launched to space on Apr 12th, 1961 at 6:07 am.'
>>>
>>> re.findall('\d{2}?', TEXT)
['12', '19', '61', '07']
>>>
>>> re.findall('\d{2}', TEXT)
['12', '19', '61', '07']

9.5.6. Special

>>> import re
>>>
>>>
>>> TEXT = 'Yuri Gagarin launched to space on Apr 12th, 1961 at 6:07 am.'
>>>
>>> re.findall('\d{0,}', TEXT) == re.findall('\d*', TEXT)
True
>>>
>>> re.findall('\d{1,}', TEXT) == re.findall('\d+', TEXT)
True
>>> import re
>>>
>>>
>>> TEXT = 'Yuri Gagarin launched to space on Apr 12th, 1961 at 6:07 am.'
>>>
>>> re.findall('\d+', TEXT)
['12', '1961', '6', '07']
>>>
>>> re.findall('\d*', TEXT)
['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '12', '', '', '', '', '1961', '', '', '', '', '6', '', '07', '', '', '', '', '']

9.5.7. Use Case - Float

>>> import re
>>>
>>>
>>> TEXT = 'Pi number is 3.1415...'
>>>
>>> pi = re.findall('\d+\.\d+', TEXT)
>>> pi
['3.1415']

9.5.8. Use Case - Time

>>> import re
>>>
>>>
>>> TEXT = 'Yuri Gagarin launched to space on Apr 12th, 1961 at 6:07 am.'
>>>
>>> re.findall('\d\d:\d\d', TEXT)
[]
>>>
>>> re.findall('\d\d?:\d\d', TEXT)
['6:07']

9.5.9. Use Case - Date

>>> import re
>>> from datetime import datetime
>>>
>>>
>>> TEXT = 'Yuri Gagarin launched to space on Apr 12th, 1961 at 6:07 am.'
>>>
>>>
>>> result = re.findall('[A-Z][a-z]{2} \d{2}th, \d{4}', TEXT)
>>> result
['Apr 12th, 1961']
>>> datetime.strptime(result[0], '%b %dth, %Y').date()
datetime.date(1961, 4, 12)

9.5.10. 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