11.11. Regexp Non-Greedy

11.11.1. About

  • Adding ? after the qualifier makes it non-greedy

  • Non-greedy - as few as possible

  • Greedy - as many as possible

Table 11.11. 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)

11.11.2. Examples

Listing 11.15. 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 11.16. 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']

11.11.3. Assignments Moon Speech (non-greedy)

  • Complexity level: easy

  • Lines of code to write: 5 lines

  • Estimated time of completion: 10 min

  • Solution: solution/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"