6.14. Regex RE Findall
re.findall()
6.14.1. Problem
Find all uppercase letters
>>> from string import ascii_uppercase
>>>
>>> text = 'Hello World'
>>> result = [x for x in text if x in ascii_uppercase]
>>>
>>> print(result)
['H', 'W']
6.14.2. Solution
>>> import re
>>>
>>> text = 'Hello World'
>>> result = re.findall(r'[A-Z]', text)
>>>
>>> print(result)
['H', 'W']
6.14.3. Use Case - 1
Usage of re.findall()
:
>>> import re
>>>
>>>
>>> HTML = """
... <h1>My Header</h1>
... <p>First Paragraph</p>
... <p>Second Paragraph</p>
... <p>Third Paragraph</p>
... """
>>>
>>> re.findall(r'<h1>(.*)</h1>', HTML)
['My Header']
>>>
>>> re.findall(r'<p>(.*)</p>', HTML)
['First Paragraph', 'Second Paragraph', 'Third Paragraph']
6.14.4. Use Case - 2
Find all JIRA issue keys in commit message
>>> import re
>>>
>>>
>>> TEXT = 'MYPROJ-1337, MYPROJ-69 removed obsolete comments'
>>> issuekey = r'[A-Z]{2,10}-[0-9]{1,6}'
>>>
>>> re.findall(issuekey, TEXT)
['MYPROJ-1337', 'MYPROJ-69']
6.14.5. Use Case - 3
Find All Adverbs
>>> import re
>>>
>>>
>>> TEXT = 'He was carefully disguised but captured quickly by police.'
>>> adverbs = r'\w+ly'
>>>
>>> re.findall(adverbs, TEXT)
['carefully', 'quickly']
6.14.6. Assignments
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Re Findall Date
# - Difficulty: easy
# - Lines: 2
# - Minutes: 5
# %% English
# 1. Define `result: list[str]` with dates in US long format:
# example: 'October 4, 1957', 'April 12, 1961', 'May 5, 1961'
# 2. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: list[str]` z datami w formacie US long:
# przykład: 'October 4, 1957', 'April 12, 1961', 'May 5, 1961'
# 2. Uruchom doctesty - wszystkie muszą się powieść
# %% References
# [1] Wikipedia Apollo 11,
# URL: https://en.wikipedia.org/wiki/Apollo_11
# Year: 2019
# Retrieved: 2019-12-14
# %% Hints
# - `re.findall()`
# - `re.MULTILINE`
# - positional or named groups
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from pprint import pprint
>>> pprint(result)
[('October', '4', '1957'),
('April', '12', '1961'),
('May', '5', '1961'),
('May', '25', '1961'),
('September', '12', '1962'),
('September', '12', '1962')]
"""
import re
DATA = """In the late 1950s and early 1960s, the United States was engaged in
the Cold War, a geopolitical rivalry with the Soviet Union. On
October 4, 1957, the Soviet Union launched Sputnik 1, the first
artificial satellite. This surprise success fired fears and
imaginations around the world. It demonstrated that the Soviet Union
had the capability to deliver nuclear weapons over intercontinental
distances, and challenged American claims of military, economic and
technological superiority. This precipitated the Sputnik crisis,
and triggered the Space Race. President Dwight D. Eisenhower
responded to the Sputnik challenge by creating the National
Aeronautics and Space Administration (NASA), and initiating Project
Mercury, which aimed to launch a man into Earth orbit. But on April
12, 1961, Soviet cosmonaut Yuri Gagarin became the first person in
space, and the first to orbit the Earth. Nearly a month later,
on May 5, 1961, Alan Shepard became the first American in space,
completing a 15-minute suborbital journey. After being recovered
from the Atlantic Ocean, he received a congratulatory telephone call
from Eisenhower's successor, John F. Kennedy. Since the Soviet Union
had higher lift capacity launch vehicles, Kennedy chose, from among
options presented by NASA, a challenge beyond the capacity of the
existing generation of rocketry, so that the US and Soviet Union
would be starting from a position of equality. A crewed mission to
the Moon would serve this purpose. On May 25, 1961, Kennedy
addressed the United States Congress on \"Urgent National Needs\"
and declared: I believe that this nation should commit itself to
achieving the goal, before this decade [1960s] is out, of landing a
man on the Moon and returning him safely to the Earth. No single
space project in this period will be more impressive to mankind,
or more important for the long-range exploration of space; and none
will be so difficult or expensive to accomplish. We propose to
accelerate the development of the appropriate lunar space craft. We
propose to develop alternate liquid and solid fuel boosters,
much larger than any now being developed, until certain which is
superior. We propose additional funds for other engine development
and for unmanned explorations—explorations which are particularly
important for one purpose which this nation will never overlook: the
survival of the man who first makes this daring flight. But in a
very real sense, it will not be one man going to the Moon—if we make
this judgment affirmatively, it will be an entire nation. For all of
us must work to put him there. — Kennedy's speech to Congress On
September 12, 1962, Kennedy delivered another speech before a crowd
of about 40,000 people in the Rice University football stadium in
Houston, Texas. A widely quoted refrain from the middle portion of
the speech reads as follows: Kennedy, in a blue suit and tie,
speaks at a wooden podium bearing the seal of the President of the
United States. Vice President Lyndon Johnson and other dignitaries
stand behind him. President John F. Kennedy speaking at Rice
University on September 12, 1962 There is no strife, no prejudice,
no national conflict in outer space as yet. Its hazards are hostile
to us all. Its conquest deserves the best of all mankind, and its
opportunity for peaceful cooperation may never come again. But why,
some say, the Moon? Why choose this as our goal? And they may well
ask, why climb the highest mountain? Why, 35 years ago, fly the
Atlantic? Why does Rice play Texas? We choose to go to the Moon! We
choose to go to the Moon ... We choose to go to the Moon in this
decade and do the other things, not because they are easy,
but because they are hard; because that goal will serve to organize
and measure the best of our energies and skills, because that
challenge is one that we are willing to accept, one we are unwilling
to postpone, and one we intend to win, and the others, too."""
# Use re.findall() to get dates in US format, i.e. "April 12, 1961"
# type: list[str]
result = ...