5.11. Regexp Non-Greedy

5.11.1. About

  • Adding ? after the qualifier makes it non-greedy

  • Non-greedy - as few as possible

  • Greedy - as many as possible

Table 5.13. Regular Expression Greedy and Non-Greedy Qualifiers




zero or one (greedy)


zero or more (greedy)


one or more (greedy)


zero or one (non greedy)


zero or more (non greedy)


one or more (non greedy)

5.11.2. Examples

Listing 5.76. Usage of greedy and non-greedy search in re.findall()
import re

TEXT = '<strong>Ehlo World</strong>'

re.findall(r'<.*>', TEXT)         # ['<strong>Ehlo World</strong>']
re.findall(r'<.*?>', TEXT)        # ['<strong>', '</strong>']
Listing 5.77. Usage of greedy and non-greedy search with groups
re.findall(r'<(.*)>', TEXT)       # ['strong>Ehlo World</strong']
re.findall(r'<(.*?)>', TEXT)      # ['strong', '/strong']
re.findall(r'</?(.*?)>', TEXT)    # ['strong', 'strong']

5.11.3. Assignments Regexp Greedy Moon Speech

  • Complexity level: easy

  • Lines of code to write: 5 lines

  • Estimated time of completion: 10 min

  • Solution: solution/regexp_greedy_moon_speech.py

  • References: "Moon Speech" by John F. Kennedy at Rice Stadium, Houston, TX on 1962-09-12 [re-1]

  1. Download "Moon Speech" text data/moon_speech.html

  2. Save as moon_speech.html

  3. Using re.findall() and non-greedy qualifier split text by paragraphs

  4. Print paragraph starting with "We choose to go to the moon"

  1. Pobierz tekst przemówienia "Moon Speech" data/moon_speech.html

  2. Zapisz jako moon_speech.html

  3. Za pomocą re.findall() i non-greedy qualifier podziel tekst na paragrafy

  4. Wyświetl paragraf zaczynający się od słów "We choose to go to the moon"