6.2. Regex Syntax Literal

  • Literals specifies what to find.

  • a - Exact

  • a|b - Alternative

6.2.1. SetUp

>>> import re

6.2.2. Exact

  • a - Exact

>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sun, Jan 2nd, 2000 at 12:00 AM'

Regular expressions allows to find exact matches:

>>> re.findall(r'a', TEXT)
['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']

Note, that regular expressions are case sensitive (unless re.IGNORECASE flag is present. More information in Syntax Flags):

>>> re.findall(r'A', TEXT)
['A']

Note, that regular expressions are used to search in text, therefore in case of searching for a number it will return a strings with numbers in it:

>>> re.findall(r'1', TEXT)
['1']

Python re.findall() function will return empty list if none match was found:

>>> re.findall(r'x', TEXT)
[]

6.2.3. Exact Alternate

  • a|b - letter a or b (also works with expressions)

>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sun, Jan 2nd, 2000 at 12:00 AM'

Alternative allows to search for two or more possible matches:

>>> re.findall(r'a|b', TEXT)
['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']

It can find more than two matches:

>>> re.findall(r'a|b|c|d', TEXT)
['a', 'a', 'a', 'a', 'a', 'a', 'c', 'd', 'a', 'd', 'a']
>>>
>>> re.findall(r'1|2|3', TEXT)
['2', '2', '1', '2']

It will work for both numbers, characters or any other object:

>>> re.findall(r'a|b|c|d|1|2|3', TEXT)
['a', 'a', 'a', 'a', 'a', 'a', 'c', 'd', 'a', '2', 'd', '2', 'a', '1', '2']

Examples:

>>> re.findall(r'a|e', TEXT)
['a', 'a', 'a', 'e', 'a', 'e', 'a', 'a', 'e', 'e', 'e', 'a', 'a']
>>>
>>> re.findall(r'a|e|i|o|u|y', TEXT)  
['a', 'i', 'o', 'a', 'a', 'e', 'y', 'a', 'e', 'y', 'a', 'a', 'o',
 'e', 'e', 'i', 'e', 'o', 'u', 'a', 'a']

6.2.4. Use Case - 1

>>> import re
>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sun, Jan 2nd, 2000 at 12:00 AM'
>>> re.findall(r'st|nd|rd|th', TEXT)
['nd']
>>> re.findall(r'[st|nd|rd|th]', TEXT)
['r', 'r', 't', 'n', 't', 'n', 'n', 's', 'r', 'd', 'n', 'n', 'n', 'n', 'd', 't']
>>> re.findall(r'[stndrdth]', TEXT)
['r', 'r', 't', 'n', 't', 'n', 'n', 's', 'r', 'd', 'n', 'n', 'n', 'n', 'd', 't']

6.2.5. 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 Syntax Literals
# - Difficulty: easy
# - Lines: 3
# - Minutes: 2

# %% English
# 1. Define `result: str` with regular expression to find:
#    - all letters `a` (lowercase)
#    - all letters `A` (uppercase)
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj `result: str` z wyrażeniem regularnym aby wyszukać:
#    - wszystkie litery `a` (małe)
#    - wszystkie litery `A` (duże)
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% References
# [1] Authors: Wikipedia contributors
#     Title: Apollo 11
#     Publisher: Wikipedia
#     Year: 2019
#     Retrieved: 2019-12-14
#     URL: https://en.wikipedia.org/wiki/Apollo_11

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> from pprint import pprint

>>> result = re.findall(result_a, DATA)
>>> pprint(result, compact=True, width=72)
['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']

>>> result = re.findall(result_b, DATA)
>>> pprint(result, compact=True, width=72)
['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A']
"""

import re

DATA = """Apollo 11 was the American spaceflight that first landed
humans on the Moon. Commander (CDR) Neil Armstrong and lunar module
pilot (LMP) Buzz Aldrin landed the Apollo Lunar Module (LM) Eagle on
July 20th, 1969 at 20:17 UTC, and Armstrong became the first person
to step (EVA) onto the Moon's surface (EVA) 6 hours 39 minutes later,
on July 21st, 1969 at 02:56:15 UTC. Aldrin joined him 19 minutes later.
They spent 2 hours 31 minutes exploring the site they had named
Tranquility Base upon landing. Armstrong and Aldrin collected 47.5 pounds
(21.5 kg) of lunar material to bring back to Earth as pilot Michael Collins
(CMP) flew the Command Module (CM) Columbia in lunar orbit, and were on the
Moon's surface for 21 hours 36 minutes before lifting off to rejoin
Columbia."""

# Find all letters `a` (lowercase)
# Example: 'a', 'a', 'a', 'a', 'a', 'a', ...
# Note: define only regex pattern (str), not re.findall(..., DATA)
# type: str
result_a = r''

# Find all letters `A` (uppercase)
# Example: 'A', 'A', 'A', 'A', 'A', ...
# Note: define only regex pattern (str), not re.findall(..., DATA)
# type: str
result_b = r''


# %% 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 Syntax Literals
# - Difficulty: easy
# - Lines: 3
# - Minutes: 2

# %% English
# 1. Define `result: str` with regular expression to find:
#    - all letters `a` (lowercase) or `A` (uppercase)
#    - all digits `1` or `2` or `3`
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj `result: str` z wyrażeniem regularnym aby wyszukać:
#    - wszystkie litery `a` (małe) lub `A` (duże)
#    - wszystkie cyfry `1` lub `2` lub `3`
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% References
# [1] Authors: Wikipedia contributors
#     Title: Apollo 11
#     Publisher: Wikipedia
#     Year: 2019
#     Retrieved: 2019-12-14
#     URL: https://en.wikipedia.org/wiki/Apollo_11

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> from pprint import pprint

>>> result = re.findall(result_a, DATA)
>>> pprint(result, compact=True, width=72)
['A', 'a', 'A', 'a', 'a', 'a', 'a', 'a', 'a', 'A', 'a', 'a', 'A', 'a',
 'A', 'a', 'a', 'a', 'a', 'A', 'a', 'A', 'a', 'A', 'a', 'a', 'A', 'a',
 'a', 'a', 'a', 'a', 'a', 'A', 'a', 'A', 'a', 'a', 'a', 'a', 'a', 'a',
 'a', 'a', 'a', 'a', 'a', 'a', 'a']

>>> result = re.findall(result_b, DATA)
>>> pprint(result, compact=True, width=72)
['1', '1', '2', '1', '2', '1', '3', '2', '1', '1', '2', '1', '1', '2',
 '3', '1', '2', '1', '2', '1', '3']
"""

import re

DATA = """Apollo 11 was the American spaceflight that first landed
humans on the Moon. Commander (CDR) Neil Armstrong and lunar module
pilot (LMP) Buzz Aldrin landed the Apollo Lunar Module (LM) Eagle on
July 20th, 1969 at 20:17 UTC, and Armstrong became the first person
to step (EVA) onto the Moon's surface (EVA) 6 hours 39 minutes later,
on July 21st, 1969 at 02:56:15 UTC. Aldrin joined him 19 minutes later.
They spent 2 hours 31 minutes exploring the site they had named
Tranquility Base upon landing. Armstrong and Aldrin collected 47.5 pounds
(21.5 kg) of lunar material to bring back to Earth as pilot Michael Collins
(CMP) flew the Command Module (CM) Columbia in lunar orbit, and were on the
Moon's surface for 21 hours 36 minutes before lifting off to rejoin
Columbia."""

# Find all letters `a` (lowercase) or `A` (uppercase)
# Example: 'a', 'a', 'a', 'a', 'a', ...
# Note: define only regex pattern (str), not re.findall(..., DATA)
# type: str
result_a = r''

# Find all digits `1` or `2` or `3`
# Example: '1', '1', '2', '1', '2', '1', ...
# Note: define only regex pattern (str), not re.findall(..., DATA)
# type: str
result_b = r''