7.11. Regex Class Boundary
Matches the empty string, but only at the beginning or end of a word
\b
- word boundary\B
- anything but word boundary
Examples:
\btodo\b
- performs a "whole words only" search
\Btodo\B
- pattern is fully surrounded by word characters
7.11.1. SetUp
import re
7.11.2. Problem
TEXT = 'On Sun, Jan 1st, 2000 at 12:00 AM Alice <alice@example.com> wrote'
re.findall(r'[a-z][a-z]', TEXT)
['un', 'an', 'st', 'at', 'li', 'ce', 'al', 'ic', 'ex', 'am', 'pl', 'co', 'wr', 'ot']
TEXT = 'On Sun, Jan 1st, 2000 at 12:00 AM Alice <alice@example.com> wrote'
re.findall(r' [a-z][a-z] ', TEXT)
[' at ']
7.11.3. Boundary
\b
- word boundary
TEXT = 'On Sun, Jan 1st, 2000 at 12:00 AM Alice <alice@example.com> wrote'
re.findall(r'\b[a-z][a-z]\b', TEXT)
['at']
7.11.4. Non-Boundary
\B
- anything but word boundary
TEXT = 'On Sun, Jan 1st, 2000 at 12:00 AM Alice <alice@example.com> wrote'
re.findall(r'\B[a-z][a-z]\B', TEXT)
['li', 'li', 'xa', 'mp', 'ro']
7.11.5. Both
However still on
is missing, because it has colon (no space) at the end:
TEXT = 'On Sun, Jan 1st, 2000 at 12:00 AM Alice <alice@example.com> wrote'
re.findall(r'\b[a-z][a-z]\B', TEXT)
['al', 'ex', 'co', 'wr']
TEXT = 'On Sun, Jan 1st, 2000 at 12:00 AM Alice <alice@example.com> wrote'
re.findall(r'\B[a-z][a-z]\b', TEXT)
['un', 'an', 'st', 'ce', 'ce', 'le', 'om', 'te']
7.11.6. Raw-String
Without raw-string:
TEXT = 'On Sun, Jan 1st, 2000 at 12:00 AM Alice <alice@example.com> wrote'
re.findall('\b[a-z][a-z]\b', TEXT)
[]
7.11.7. Case Study 1
import re
text = """
# TODO: poprawić coś tam
# todo: poprawić coś innego
stodoła
metodologia
ortodonta
autodoc
autodoping
"""
re.findall(r'todo', text)
# ['todo', 'todo', 'todo', 'todo', 'todo', 'todo']
re.findall(r'\Btodo\B', text)
# ['todo', 'todo', 'todo', 'todo', 'todo']
re.findall(r'\btodo\b', text)
# ['todo']
re.findall(r'\btodo\b', text, flags=re.IGNORECASE)
# ['TODO', 'todo']
7.11.8. Assignments
# %% About
# - Name: RE Syntax WordBoundary
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% 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
# %% English
# 1. Define `result: str` with regular expression pattern to find
# two uppercase letter acronyms
# example: ['LM', 'CM']
# 2. Define only regex pattern (str), not re.findall(...)
# 3. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: str` z wzorcem wyrażenia regularnego aby wyszukać
# dwu wielkoliterowe akronimy
# przykład: ['LM', 'CM']
# 2. Zdefiniuj tylko wzorzec regex (str), nie re.findall(...)
# 3. 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
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from pprint import pprint
>>> result = re.findall(result, DATA)
>>> pprint(result, compact=True, width=72)
['LM', 'CM']
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% Imports
import re
# %% Types
result: str
# %% Data
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."""
# %% Result
result = r''
# %% About
# - Name: RE Syntax WordBoundary
# - Difficulty: easy
# - Lines: 2
# - Minutes: 2
# %% 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
# %% English
# 1. Define `result_a: str` with regular expression pattern to find
# all two-letter conjunctives (two-letter words)
# example: ['on', 'on', 'at', 'to', ...]
# 2. Define `result_b: str` with regular expression pattern to find
# all two-letter pairs embedded in the words
# example: ['po', 'll', 'me', 'ri', 'ca', 'pa', 'ce', ...]
# 3. Define only regex pattern (str), not re.findall(...)
# 4. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result_a: str` z wzorcem wyrażenia regularnego aby wyszukać
# wszystkie dwu literowe spójniki (dwu-literowe słowa)
# przykład: ['on', 'on', 'at', 'to', ...]
# 2. Zdefiniuj `result_b: str` z wzorcem wyrażenia regularnego aby wyszukać
# wszystkie dwu literowe pary wbudowane w słowach
# przykład: ['po', 'll', 'me', 'ri', 'ca', 'pa', 'ce', ...]
# 3. Zdefiniuj tylko wzorzec regex (str), nie re.findall(...)
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - Use r-string
# %% References
# [1] Authors: Wikipedia contributors
# Title: Apollo 11
# Publisher: Wikipedia
# Year: 2019
# Retrieved: 2019-12-14
# URL: https://en.wikipedia.org/wiki/Apollo_11
# %% Doctests
"""
>>> 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)
['on', 'on', 'at', 'to', 'on', 'at', 'kg', 'of', 'to', 'to', 'as', 'in',
'on', 'to']
>>> result = re.findall(result_b, DATA)
>>> pprint(result, compact=True, width=72)
['po', 'll', 'me', 'ri', 'ca', 'pa', 'ce', 'fl', 'ig', 'ha', 'ir', 'an',
'de', 'um', 'an', 'oo', 'om', 'ma', 'nd', 'ei', 'rm', 'st', 'ro', 'un',
'od', 'ul', 'il', 'uz', 'ld', 'ri', 'an', 'de', 'po', 'll', 'un', 'od',
'ul', 'ag', 'ul', 'rm', 'st', 'ro', 'ec', 'am', 'ir', 'er', 'so', 'te',
'nt', 'oo', 'ur', 'fa', 'ou', 'in', 'ut', 'at', 'ul', 'ld', 'ri', 'oi',
'ne', 'in', 'ut', 'at', 'he', 'pe', 'ou', 'in', 'ut', 'xp', 'lo', 'ri',
'it', 'he', 'am', 'ra', 'nq', 'ui', 'li', 'as', 'po', 'an', 'di', 'rm',
'st', 'ro', 'ld', 'ri', 'ol', 'le', 'ct', 'ou', 'nd', 'un', 'at', 'er',
'ia', 'ri', 'ac', 'ar', 'il', 'ic', 'ha', 'ol', 'li', 'le', 'om', 'ma',
'od', 'ul', 'ol', 'um', 'bi', 'un', 'rb', 'er', 'oo', 'ur', 'fa', 'ou',
'in', 'ut', 'ef', 'or', 'if', 'ti', 'ej', 'oi', 'ol', 'um', 'bi']
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% Imports
import re
# %% Types
result_a: str
result_b: str
# %% Data
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."""
# %% Result
result_a = r''
result_b = r''
# %% About
# - Name: RE Syntax WordBoundary
# - Difficulty: easy
# - Lines: 2
# - Minutes: 2
# %% 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
# %% English
# 1. Define `result_a: str` with regular expression pattern to find
# all first three lowercase letters from each word
# example: ['spa', 'tha', 'fir', 'lan', 'hum', 'lun', ...]
# 2. Define `result_b: str` with regular expression pattern to find
# all last three lowercase letters from each word
# example: ['llo', 'can', 'ght', 'hat', 'rst', 'ded', ...]
# 3. Define only regex pattern (str), not re.findall(...)
# 4. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result_a: str` z wzorcem wyrażenia regularnego aby wyszukać
# wszystkie pierwsze trzy małe litery z każdego słowa
# przykład: ['spa', 'tha', 'fir', 'lan', 'hum', 'lun', ...]
# 2. Zdefiniuj `result_b: str` z wzorcem wyrażenia regularnego aby wyszukać
# wszystkie ostatnie trzy małe litery z każdego słowa
# przykład: ['llo', 'can', 'ght', 'hat', 'rst', 'ded', ...]
# 3. Zdefiniuj tylko wzorzec regex (str), nie re.findall(...)
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Hints
# - Use r-string
# %% References
# [1] Authors: Wikipedia contributors
# Title: Apollo 11
# Publisher: Wikipedia
# Year: 2019
# Retrieved: 2019-12-14
# URL: https://en.wikipedia.org/wiki/Apollo_11
# %% Doctests
"""
>>> 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)
['spa', 'tha', 'fir', 'lan', 'hum', 'lun', 'mod', 'pil', 'lan', 'bec',
'fir', 'per', 'ste', 'ont', 'sur', 'hou', 'min', 'lat', 'joi', 'min',
'lat', 'spe', 'hou', 'min', 'exp', 'sit', 'the', 'nam', 'upo', 'lan',
'col', 'pou', 'lun', 'mat', 'bri', 'bac', 'pil', 'fle', 'lun', 'orb',
'wer', 'sur', 'hou', 'min', 'bef', 'lif', 'rej']
>>> result = re.findall(result_b, DATA)
>>> pprint(result, compact=True, width=72)
['llo', 'can', 'ght', 'hat', 'rst', 'ded', 'ans', 'oon', 'der', 'eil',
'ong', 'nar', 'ule', 'lot', 'uzz', 'rin', 'ded', 'llo', 'nar', 'ule',
'gle', 'uly', 'ong', 'ame', 'rst', 'son', 'tep', 'nto', 'oon', 'ace',
'urs', 'tes', 'ter', 'uly', 'rin', 'ned', 'tes', 'ter', 'hey', 'ent',
'urs', 'tes', 'ing', 'ite', 'hey', 'med', 'ity', 'ase', 'pon', 'ing',
'ong', 'rin', 'ted', 'nds', 'nar', 'ial', 'ing', 'ack', 'rth', 'lot',
'ael', 'ins', 'lew', 'and', 'ule', 'bia', 'nar', 'bit', 'ere', 'oon',
'ace', 'urs', 'tes', 'ore', 'ing', 'oin', 'bia']
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% Imports
import re
# %% Types
result_a: str
result_b: str
# %% Data
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."""
# %% Result
result_a = r''
result_b = r''