6.4. Regex Syntax Anchor
.
- any character except a newline (changes meaning withre.DOTALL
)^
- start of line (changes meaning withre.MULTILINE
)$
- end of line (changes meaning withre.MULTILINE
)\A
- start of text (doesn't change meaning withre.MULTILINE
)\Z
- end of text (doesn't change meaning withre.MULTILINE
)
6.4.1. SetUp
>>> import re
6.4.2. Any Character
.
- any character except a newline (changes meaning withre.DOTALL
)
Search for letters No
followed by any character:
>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sun, Jan 2nd, 2000 at 12:00 AM'
>>> re.findall(r'.nd', TEXT)
['2nd']
Search for uppercase letter followed by any three characters:
>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sun, Jan 2nd, 2000 at 12:00 AM'
>>> re.findall(r'[A-Z]..', TEXT)
['Ema', 'Mar', 'Wat', 'Sun', 'Jan']
Example:
>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sun, Jan 2nd, 2000 at 12:00 AM'
>>> re.findall(r'Jan 2..', TEXT)
['Jan 2nd']
6.4.3. Start of Line
^
- start of a lineChanges meaning with
re.MULTILINE
Search for a capital letter at the start of a line:
>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sun, Jan 2nd, 2000 at 12:00 AM'
>>> re.findall(r'^[A-Z]', TEXT)
['E']
Search for a capital letter anywhere in text:
>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sun, Jan 2nd, 2000 at 12:00 AM'
>>> re.findall(r'[A-Z]', TEXT)
['E', 'M', 'W', 'S', 'J', 'A', 'M']
6.4.4. End of Line
$
- end of lineChanges meaning with
re.MULTILINE
Give me last characters in a line:
>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sun, Jan 2nd, 2000 at 12:00 AM'
>>> re.findall(r'.$', TEXT)
['M']
6.4.5. Start of String
\A
- start of a textDoesn't change meaning with
re.MULTILINE
Search for a capital letter in text at the start of a line:
>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sun, Jan 2nd, 2000 at 12:00 AM'
>>> re.findall(r'\A[A-Z]', TEXT)
['E']
Note, that the output is identical to Start of a Line ^
. It will differ
when re.MULTILINE
flag is present.
>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sun, Jan 2nd, 2000 at 12:00 AM'
>>> re.findall(r'^[A-Z]', TEXT)
['E']
6.4.6. End of String
\Z
- end of a textDoesn't change meaning with
re.MULTILINE
Give me last character in a text:
>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sun, Jan 2nd, 2000 at 12:00 AM'
>>> re.findall(r'.\Z', TEXT)
['M']
Note, that the output is identical to Start of a Line ^
. It will differ
when re.MULTILINE
flag is present.
>>> TEXT = 'Email from Mark Watney <mwatney@nasa.gov> received on: Sun, Jan 2nd, 2000 at 12:00 AM'
>>> re.findall(r'.$', TEXT)
['M']
6.4.7. Multiline
Multiline strings are in fact one line with multiple newline characters (
\n
)re.MULTILINE
>>> text = """First line
... Second line
... Third line"""
Start and end of the text:
>>> re.findall(r'\A.', text)
['F']
>>> re.findall(r'.\Z', text)
['e']
Start and end of the line (problem):
>>> re.findall(r'^.', text)
['F']
>>> re.findall(r'\A.', text)
['F']
Why this is not working?
>>> text
'First line\nSecond line\nThird line'
Start and end of the line (solution):
re.findall(r'^.', text, flags=re.MULTILINE) ['F', 'S', 'T']
re.findall(r'A.', text, flags=re.MULTILINE) ['F']
6.4.8. Case Study
import re
text = """Apollo 11 was a mission that first landed humans on the Moon. Neil Armstrong
was first person to step onto the moon's surface. Buzz Aldrin joined him 19
minutes later. They spent 2 hours 31 minutes exploring the Sea of Tranquility.
Both astronauts collected lunar material. Neil and Buzz were on the Moon's
surface for 21 hours 36 minutes. Pilot Michael Collins waited for them in the
lunar orbit."""
# %%
re.findall(r'\A.', text)
# ['A']
# %%
re.findall(r'.\Z', text)
# ['.']
import re
text = """Apollo 11 was a mission that first landed humans on the Moon. Neil Armstrong
was first person to step onto the moon's surface. Buzz Aldrin joined him 19
minutes later. They spent 2 hours 31 minutes exploring the Sea of Tranquility.
Both astronauts collected lunar material. Neil and Buzz were on the Moon's
surface for 21 hours 36 minutes. Pilot Michael Collins waited for them in the
lunar orbit."""
# %%
re.findall(r'^.', text)
# ['A']
# %%
re.findall(r'.$', text)
# ['.']
import re
text = """Apollo 11 was a mission that first landed humans on the Moon. Neil Armstrong
was first person to step onto the moon's surface. Buzz Aldrin joined him 19
minutes later. They spent 2 hours 31 minutes exploring the Sea of Tranquility.
Both astronauts collected lunar material. Neil and Buzz were on the Moon's
surface for 21 hours 36 minutes. Pilot Michael Collins waited for them in the
lunar orbit."""
text
"Apollo 11 was a mission that first landed humans on the Moon. Neil Armstrong\nwas first person to step onto the moon's surface. Buzz Aldrin joined him 19\nminutes later. They spent 2 hours 31 minutes exploring the Sea of Tranquility.\nBoth astronauts collected lunar material. Neil and Buzz were on the Moon's\nsurface for 21 hours 36 minutes. Pilot Michael Collins waited for them in the\nlunar orbit."
# %%
re.findall(r'\A.', text, flags=re.MULTILINE)
# ['A']
# %%
re.findall(r'.\Z', text, flags=re.MULTILINE)
# ['.']
# %%
re.findall(r'^.', text, flags=re.MULTILINE)
# ['A', 'w', 'm', 'B', 's', 'l']
# %%
re.findall(r'.$', text, flags=re.MULTILINE)
# ['g', '9', '.', 's', 'e', '.']
6.4.9. Use Case - 1
abc.e
- text abc then any character followed by letter e
6.4.10. 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 Anchor
# - Difficulty: easy
# - Lines: 2
# - Minutes: 2
# %% English
# 1. Define `result: str` with regular expression to find:
# - all characters in text
# 2. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: str` z wyrażeniem regularnym aby wyszukać:
# - wszystkie znaki w tekście
# 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'
>>> result = re.findall(result, DATA, flags=re.MULTILINE)
>>> from pprint import pprint
>>> pprint(result, compact=True)
['A', 'p', 'o', 'l', 'l', 'o', ' ', '1', '1', ' ', 'w', 'a', 's', ' ', 't', 'h',
'e', ' ', 'A', 'm', 'e', 'r', 'i', 'c', 'a', 'n', ' ', 's', 'p', 'a', 'c', 'e',
'f', 'l', 'i', 'g', 'h', 't', ' ', 't', 'h', 'a', 't', ' ', 'f', 'i', 'r', 's',
't', ' ', 'l', 'a', 'n', 'd', 'e', 'd', 'h', 'u', 'm', 'a', 'n', 's', ' ', 'o',
'n', ' ', 't', 'h', 'e', ' ', 'M', 'o', 'o', 'n', '.', ' ', 'C', 'o', 'm', 'm',
'a', 'n', 'd', 'e', 'r', ' ', '(', 'C', 'D', 'R', ')', ' ', 'N', 'e', 'i', 'l',
' ', 'A', 'r', 'm', 's', 't', 'r', 'o', 'n', 'g', ' ', 'a', 'n', 'd', ' ', 'l',
'u', 'n', 'a', 'r', ' ', 'm', 'o', 'd', 'u', 'l', 'e', 'p', 'i', 'l', 'o', 't',
' ', '(', 'L', 'M', 'P', ')', ' ', 'B', 'u', 'z', 'z', ' ', 'A', 'l', 'd', 'r',
'i', 'n', ' ', 'l', 'a', 'n', 'd', 'e', 'd', ' ', 't', 'h', 'e', ' ', 'A', 'p',
'o', 'l', 'l', 'o', ' ', 'L', 'u', 'n', 'a', 'r', ' ', 'M', 'o', 'd', 'u', 'l',
'e', ' ', '(', 'L', 'M', ')', ' ', 'E', 'a', 'g', 'l', 'e', ' ', 'o', 'n', 'J',
'u', 'l', 'y', ' ', '2', '0', 't', 'h', ',', ' ', '1', '9', '6', '9', ' ', 'a',
't', ' ', '2', '0', ':', '1', '7', ' ', 'U', 'T', 'C', ',', ' ', 'a', 'n', 'd',
' ', 'A', 'r', 'm', 's', 't', 'r', 'o', 'n', 'g', ' ', 'b', 'e', 'c', 'a', 'm',
'e', ' ', 't', 'h', 'e', ' ', 'f', 'i', 'r', 's', 't', ' ', 'p', 'e', 'r', 's',
'o', 'n', 't', 'o', ' ', 's', 't', 'e', 'p', ' ', '(', 'E', 'V', 'A', ')', ' ',
'o', 'n', 't', 'o', ' ', 't', 'h', 'e', ' ', 'M', 'o', 'o', 'n', "'", 's', ' ',
's', 'u', 'r', 'f', 'a', 'c', 'e', ' ', '(', 'E', 'V', 'A', ')', ' ', '6', ' ',
'h', 'o', 'u', 'r', 's', ' ', '3', '9', ' ', 'm', 'i', 'n', 'u', 't', 'e', 's',
' ', 'l', 'a', 't', 'e', 'r', ',', 'o', 'n', ' ', 'J', 'u', 'l', 'y', ' ', '2',
'1', 's', 't', ',', ' ', '1', '9', '6', '9', ' ', 'a', 't', ' ', '0', '2', ':',
'5', '6', ':', '1', '5', ' ', 'U', 'T', 'C', '.', ' ', 'A', 'l', 'd', 'r', 'i',
'n', ' ', 'j', 'o', 'i', 'n', 'e', 'd', ' ', 'h', 'i', 'm', ' ', '1', '9', ' ',
'm', 'i', 'n', 'u', 't', 'e', 's', ' ', 'l', 'a', 't', 'e', 'r', '.', 'T', 'h',
'e', 'y', ' ', 's', 'p', 'e', 'n', 't', ' ', '2', ' ', 'h', 'o', 'u', 'r', 's',
' ', '3', '1', ' ', 'm', 'i', 'n', 'u', 't', 'e', 's', ' ', 'e', 'x', 'p', 'l',
'o', 'r', 'i', 'n', 'g', ' ', 't', 'h', 'e', ' ', 's', 'i', 't', 'e', ' ', 't',
'h', 'e', 'y', ' ', 'h', 'a', 'd', ' ', 'n', 'a', 'm', 'e', 'd', 'T', 'r', 'a',
'n', 'q', 'u', 'i', 'l', 'i', 't', 'y', ' ', 'B', 'a', 's', 'e', ' ', 'u', 'p',
'o', 'n', ' ', 'l', 'a', 'n', 'd', 'i', 'n', 'g', '.', ' ', 'A', 'r', 'm', 's',
't', 'r', 'o', 'n', 'g', ' ', 'a', 'n', 'd', ' ', 'A', 'l', 'd', 'r', 'i', 'n',
' ', 'c', 'o', 'l', 'l', 'e', 'c', 't', 'e', 'd', ' ', '4', '7', '.', '5', ' ',
'p', 'o', 'u', 'n', 'd', 's', '(', '2', '1', '.', '5', ' ', 'k', 'g', ')', ' ',
'o', 'f', ' ', 'l', 'u', 'n', 'a', 'r', ' ', 'm', 'a', 't', 'e', 'r', 'i', 'a',
'l', ' ', 't', 'o', ' ', 'b', 'r', 'i', 'n', 'g', ' ', 'b', 'a', 'c', 'k', ' ',
't', 'o', ' ', 'E', 'a', 'r', 't', 'h', ' ', 'a', 's', ' ', 'p', 'i', 'l', 'o',
't', ' ', 'M', 'i', 'c', 'h', 'a', 'e', 'l', ' ', 'C', 'o', 'l', 'l', 'i', 'n',
's', '(', 'C', 'M', 'P', ')', ' ', 'f', 'l', 'e', 'w', ' ', 't', 'h', 'e', ' ',
'C', 'o', 'm', 'm', 'a', 'n', 'd', ' ', 'M', 'o', 'd', 'u', 'l', 'e', ' ', '(',
'C', 'M', ')', ' ', 'C', 'o', 'l', 'u', 'm', 'b', 'i', 'a', ' ', 'i', 'n', ' ',
'l', 'u', 'n', 'a', 'r', ' ', 'o', 'r', 'b', 'i', 't', ',', ' ', 'a', 'n', 'd',
' ', 'w', 'e', 'r', 'e', ' ', 'o', 'n', ' ', 't', 'h', 'e', 'M', 'o', 'o', 'n',
"'", 's', ' ', 's', 'u', 'r', 'f', 'a', 'c', 'e', ' ', 'f', 'o', 'r', ' ', '2',
'1', ' ', 'h', 'o', 'u', 'r', 's', ' ', '3', '6', ' ', 'm', 'i', 'n', 'u', 't',
'e', 's', ' ', 'b', 'e', 'f', 'o', 'r', 'e', ' ', 'l', 'i', 'f', 't', 'i', 'n',
'g', ' ', 'o', 'f', 'f', ' ', 't', 'o', ' ', 'r', 'e', 'j', 'o', 'i', 'n', 'C',
'o', 'l', 'u', 'm', 'b', 'i', '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 characters in text
# Example: ['A', 'p', 'o', 'l', 'l', 'o', ' ', '1', '1', ...]
# Note: define only regex pattern (str), not re.findall(...)
# type: str
result = 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 Anchor
# - Difficulty: easy
# - Lines: 2
# - Minutes: 2
# %% English
# 1. Define `result: str` with regular expression to find:
# - character at the beginning of a text
# - character at the beginning of each line
# 2. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: str` z wyrażeniem regularnym aby wyszukać:
# - znak na początku tekstu
# - znak na początku każdej linii
# 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, flags=re.MULTILINE)
>>> pprint(result, compact=True)
['A']
>>> result = re.findall(result_b, DATA, flags=re.MULTILINE)
>>> pprint(result, compact=True)
['A', 'h', 'p', 'J', 't', 'o', 'T', 'T', '(', '(', 'M', 'C']
"""
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 character at the beginning of a text
# Example: 'A'
# Note: define only regex pattern (str), not re.findall(...)
# type: str
result_a = r''
# Find character at the beginning of each line
# Example: 'A', 'h', 'p', 'J', 't', 'o', 'T', 'B', 'o', 'f', 'M', 'C'
# Note: define only regex pattern (str), not re.findall(...)
# 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 Anchor
# - Difficulty: easy
# - Lines: 2
# - Minutes: 2
# %% English
# 1. Define `result: str` with regular expression to find:
# - character at the end of a text
# - character at the end of each line
# 2. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: str` z wyrażeniem regularnym aby wyszukać:
# - znak na końcu tekstu
# - znak na końcu każdej linii
# 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, flags=re.MULTILINE)
>>> pprint(result, compact=True)
['.']
>>> result = re.findall(result_b, DATA, flags=re.MULTILINE)
>>> pprint(result, compact=True)
['d', 'e', 'n', 'n', ',', '.', 'd', 's', 's', 'e', 'n', '.']
"""
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 character at the end of a text
# Example: '.'
# Note: define only regex pattern (str), not re.findall(...)
# type: str
result_a = r''
# Find character at the end of each line
# Example: 'd', 'e', 'n', 'n', ',', '.'
# Note: define only regex pattern (str), not re.findall(...)
# type: str
result_b = r''