5.4. Regexp Findall, Finditer

5.4.1. About

  • re.findall()

  • re.finditer()

5.4.2. Examples

Listing 863. Usage of re.findall() and re.finditer()
import re


PATTERN = r'[A-Z]{2,10}-[0-9]{1,6}'
INPUT = 'MYPROJ-1337, MYPROJ-997 removed obsolete comments'

re.findall(PATTERN, INPUT)
# ['MYPROJ-1337', 'MYPROJ-997']
Listing 864. Finding All Adverbs
import re


TEXT = 'He was carefully disguised but captured quickly by police.'
ADVERBS = r'\w+ly'

re.findall(ADVERBS, TEXT)
# ['carefully', 'quickly']

5.4.3. Assignments

5.4.3.1. Finding Dates

  • Complexity level: easy

  • Lines of code to write: 5 lines

  • Estimated time of completion: 10 min

  • Solution: solution/find_dates.py

  • References: [re-2]

English
  1. Download data/apollo11-content.txt and save as regex_dates.txt

  2. Using regular expressions find dates in US format (example: "April 12, 1961")

  3. Print all dates

Polish
  1. Pobierz data/apollo11-content.txt i zapisz jako regex_dates.txt

  2. Używając wyrażeń regularnych wyszukaj dat w formacie US (przykład: "April 12, 1961")

  3. Wyświetl wszystkie daty

Output
print(output)
# ['October 4, 1957',
#  'April 12, 1961',
#  'May 5, 1961',
#  'May 25, 1961',
#  'September 12, 1962',
#  'September 12, 1962']