7.16. Regex Quantifier Shorthand

7.16.1. SetUp

>>> import re

7.16.2. 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})

Plus:

>>> string = 'On Sun, Jan 1st, 2000 at 12:00 AM Alice <alice@example.com> wrote'
>>> re.findall(r'\d{1,}', string)
['1', '2000', '12', '00']
>>> string = 'On Sun, Jan 1st, 2000 at 12:00 AM Alice <alice@example.com> wrote'
>>> re.findall(r'\d+', string)
['1', '2000', '12', '00']

Star:

>>> string = 'On Sun, Jan 1st, 2000 at 12:00 AM Alice <alice@example.com> wrote'
>>> re.findall(r'\d{0,}', string)
['', '', '', '', '', '', '', '', '', '', '', '', '1', '', '', '',
 '', '2000', '', '', '', '', '12', '', '00', '', '', '', '', '',
 '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
 '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']
>>> string = 'On Sun, Jan 1st, 2000 at 12:00 AM Alice <alice@example.com> wrote'
>>> re.findall(r'\d*', string)
['', '', '', '', '', '', '', '', '', '', '', '', '1', '', '', '',
 '', '2000', '', '', '', '', '12', '', '00', '', '', '', '', '',
 '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
 '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']

Question mark:

>>> string = 'On Sun, Jan 1st, 2000 at 12:00 AM Alice <alice@example.com> wrote'
>>> re.findall(r'\d{0,1}', string)
['', '', '', '', '', '', '', '', '', '', '', '', '1', '', '', '',
 '', '2', '0', '0', '0', '', '', '', '', '1', '2', '', '0', '0',
 '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
 '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
 '', '', '', '']
>>> string = 'On Sun, Jan 1st, 2000 at 12:00 AM Alice <alice@example.com> wrote'
>>> re.findall(r'\d?', string)
['', '', '', '', '', '', '', '', '', '', '', '', '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 strings.

7.16.3. 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}?)

>>> string = 'On Sun, Jan 1st, 2000 at 12:00 AM Alice <alice@example.com> wrote'
>>> re.findall(r'\d{1,}?', string)
['1', '2', '0', '0', '0', '1', '2', '0', '0']
>>> string = 'On Sun, Jan 1st, 2000 at 12:00 AM Alice <alice@example.com> wrote'
>>> re.findall(r'\d+?', string)
['1', '2', '0', '0', '0', '1', '2', '0', '0']

Star:

>>> string = 'On Sun, Jan 1st, 2000 at 12:00 AM Alice <alice@example.com> wrote'
>>> re.findall(r'\d{0,}?', string)
['', '', '', '', '', '', '', '', '', '', '', '', '', '1', '', '', '',
 '', '', '2', '', '0', '', '0', '', '0', '', '', '', '', '', '1', '',
 '2', '', '', '0', '', '0', '', '', '', '', '', '', '', '', '', '', '',
 '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
 '', '', '', '', '', '', '', '']
>>> string = 'On Sun, Jan 1st, 2000 at 12:00 AM Alice <alice@example.com> wrote'
>>> re.findall(r'\d*?', string)
['', '', '', '', '', '', '', '', '', '', '', '', '', '1', '', '', '',
 '', '', '2', '', '0', '', '0', '', '0', '', '', '', '', '', '1', '',
 '2', '', '', '0', '', '0', '', '', '', '', '', '', '', '', '', '', '',
 '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
 '', '', '', '', '', '', '']

Question mark:

>>> string = 'On Sun, Jan 1st, 2000 at 12:00 AM Alice <alice@example.com> wrote'
>>> re.findall(r'\d{0,1}?', string)
['', '', '', '', '', '', '', '', '', '', '', '', '', '1', '', '', '',
 '', '', '2', '', '0', '', '0', '', '0', '', '', '', '', '', '1', '',
 '2', '', '', '0', '', '0', '', '', '', '', '', '', '', '', '', '', '',
 '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
 '', '', '', '', '', '', '']
>>> string = 'On Sun, Jan 1st, 2000 at 12:00 AM Alice <alice@example.com> wrote'
>>> re.findall(r'\d??', string)
['', '', '', '', '', '', '', '', '', '', '', '', '', '1', '', '', '',
 '', '', '2', '', '0', '', '0', '', '0', '', '', '', '', '', '1', '',
 '2', '', '', '0', '', '0', '', '', '', '', '', '', '', '', '', '',
 '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
 '', '', '', '', '', '', '', '', '']