You should be using raw strings in your code

>>> x = 'one two three'
>>> y = re.search(r"\btwo\b", x)
>>> y
<_sre.SRE_Match object at 0x100418a58>
>>> 

Also, why don't you try

word = 'two'
re.compile(r'\b%s\b' % word, re.I)

Output:

>>> word = 'two'
>>> k = re.compile(r'\b%s\b' % word, re.I)
>>> x = 'one two three'
>>> y = k.search( x)
>>> y
<_sre.SRE_Match object at 0x100418850>
Answer from pyfunc on Stack Overflow
🌐
Python documentation
docs.python.org › 3 › library › re.html
re — Regular expression operations
A word is defined as a sequence of word characters. Note that formally, \b is defined as the boundary between a \w and a \W character (or vice versa), or between \w and the beginning or end of the string.
Discussions

What is a word boundary in regex? - Stack Overflow
I'm trying to use regexes to match space-separated numbers. I can't find a precise definition of \b ("word boundary"). I had assumed that -12 would be an "integer word" (matched... More on stackoverflow.com
🌐 stackoverflow.com
Add (7) Word Space - Exclude Non-Alphanumeric Characters
Support for the Renaming Utility - www.bulkrenameutility.co.uk · The requested topic does not exist More on bulkrenameutility.co.uk
🌐 bulkrenameutility.co.uk
Can someone explain to me what "\b" does in reg expressions?
It's a word boundary . There are three different positions that qualify as word boundaries: - Before the first character in the string, if the first character is a word character. - After the last character in the string, if the last character is a word character. - Between two characters in the string, where one is a word character and the other is not a word character. Play with it in regex101 to get a better feel for what it does. More on reddit.com
🌐 r/learnpython
16
138
June 28, 2021
Understanding \b word boundaries of regex syntax
I typed up that text from your screenshot and saved it in a file: $ cat testfile Houston, we have a problem with "string one" and "string two". Please respond The "word" characters are here: $ perl -pe 's/\w/#/g' testfile #######, ## #### # ####### #### "###### ###" ### "###### ###". ###### ####### The \b are in these spots here: $ perl -pe 's/\b/#/g' testfile #Houston#, #we# #have# #a# #problem# #with# "#string# #one#" #and# "#string# #two#". #Please# #respond# When you search for ., you find any character, including a space character. That's where you then get spaces and " as a result when they are next to a \b spot. More on reddit.com
🌐 r/linuxquestions
5
7
June 17, 2021
🌐
Rexegg
rexegg.com › regex-boundaries.php
Regex Boundaries—Word Boundaries and More
If you want to create a "real word boundary" (where a word is only allowed to have letters), see the recipe below in the section on DYI boundaries. (direct link) Difference between Engines As you can see on the regex cheat sheet, \b behaves differently depending on the engine: ✽ In PCRE (PHP, R…) with the Unicode mode turned off, JavaScript and Python 2.7, it matches where only one side is an ASCII letter, digit or underscore.
🌐
Python Tutorial
pythontutorial.net › home › python regex › python regex word boundary
Python Regex Word Boundary
June 1, 2023 - In this example, the '\bPython\b' pattern matches the whole word Python in the string 'CPython is the implementation of Python in C'. The \b represents a word boundary in a string.
🌐
Imperial College London
python.pages.doc.ic.ac.uk › 2022 › lessons › regex › 04-boundary › 03-boundary.html
Advanced Lesson 1: Regular Expressions > Word boundary | Python Programming (70053 Autumn Term 2022/2023) | Department of Computing | Imperial College London
The solution is to use the word boundary marker (\b). The regular expression "red\b" (with an end of word marker) will prevent a match to "reddit". The regular expression "\bred" (with a beginning of word marker) will prevent a match to "hundred". You can combine both "\bred\b" to restrict ...
🌐
Regular-Expressions.info
regular-expressions.info › wordboundaries.html
Regex Tutorial: \b Word Boundaries
In regular expressions, \b anchors the regex at a word boundary or the position between a word and a non-word character, or vice versa.
Find elsewhere
🌐
freeCodeCamp
freecodecamp.org › news › what-does-b-in-regex-mean-word-boundary-and-non-word-boundary-metacharacters
What Does B in RegEx Mean? Word Boundary and Non-word Boundary Metacharacters
March 8, 2023 - The small letter \b word boundary indicates that a pattern is bounded by a non-word character. Non-word characters are all characters apart from numbers, letters, and underscore (_).
🌐
Code.mu
code.mu › en › python › book › supreme › regular › words-boundary
Word Boundaries in Python Regular Expressions
The '\\b' command references a word boundary, while '\B' or '\\B' do not reference a boundary. Note that Python strictly uses double-slash escaping for word boundaries, while other commands allow a single slash.
🌐
Google
developers.google.com › google for education › python › python regular expressions
Python Regular Expressions | Python Education | Google for Developers
\b -- boundary between word and non-word · \s -- (lowercase s) matches a single whitespace character -- space, newline, return, tab, form [ \n\r\t\f]. \S (upper case S) matches any non-whitespace character.
🌐
University of Edinburgh
geos.ed.ac.uk › ~bmg › software › Perl Books › RegExp_perl_python_java_etc.pdf pdf
Regular Expression Pocket Reference, Second Edition
o Regexes and Pattern Matching · | 5 · ve specific shorthands for the · character, form feed, newline, ntal · tab, and · vertical · tab · \n is often a shorthand for the · is usually LF (012 octal), but · octal), depending on the oper- many implementations use \b · and word boundary (position ·
🌐
Imperial College London
python.pages.doc.ic.ac.uk › 2021 › lessons › regex › 04-boundary › 02-eos.html
Advanced Lesson 1: Regular Expressions > End of sentence | Python Programming (70053 Autumn Term 2021/2022) | Department of Computing | Imperial College London
70053 Autumn Term 2021/2022 · Let’s say you are a "tea" person, and want to search for phrases that only mentions "tea". But the following regular expression matches the phrase "tea and coffee" (even though it only extracts the "tea" part)
🌐
Graham Watts
grahamwatts.co.uk › home
Introducing Regular Expressions | Graham Watts
January 1, 2024 - This is useful when working with multi-line strings, such as log files, where you want to match the start and end of the entire string, but not the start and end of each line. You can also use \b and \B to match word boundaries.
🌐
Chryswoods
chryswoods.com › intermediate_python › regexp.html
chryswoods.com | Regular Expressions in Python
This will search for the lines that contain the word dream and will print them, e.g. ... re.search is used to search, in this case for the string dream in string line. If the text is found, then re.search returns True, else it returns False. Note that we put an r in front of the search string. This is to tell Python that this is a raw string which should not be escaped (more about this later..)
🌐
Cheatography
cheatography.com › sahusourabh › cheat-sheets › python-regular-expression-cheatsheet
PYTHON REGULAR EXPRESSION CHEATSHEET Cheat Sheet by sahusourabh - Download free from Cheatography - Cheatography.com: Cheat Sheets For Every Occasion
Regular expressions in one of the important topic in python. It is widely used by developer and testers. If you want to play around with data. One should know regular expressions.
🌐
Dyalog
help.dyalog.com › latest › Content › Language › Appendices › PCRE Regular Expression Syntax Summary.htm
PCRE Regular Expression Syntax Summary
0 or 1, lazy * 0 or more, greedy *+ 0 or more, possessive *? 0 or more, lazy + 1 or more, greedy ++ 1 or more, possessive +? 1 or more, lazy {n} exactly n {n,m} at least n, no more than m, greedy {n,m}+ at least n, no more than m, possessive {n,m}? at least n, no more than m, lazy {n,} n or more, greedy {n,}+ n or more, possessive {n,}? n or more, lazy ANCHORS AND SIMPLE ASSERTIONS \b word boundary \B not a word boundary ^ start of subject also after internal newline in multiline mode \A start of subject $ end of subject also before newline at end of subject also before internal newline in multiline mode \Z end of subject also before newline at end of subject \z end of subject \G first matching position in subject MATCH POINT RESET \K reset start of match \K is honoured in positive assertions, but ignored in negative ones.
🌐
Hardman Trust
directory.hardmantrust.org.uk › HomePages › uploaded-files › V69768 › RegularExpressionCheatSheet.pdf
Regular Expression Cheat Sheet
By clicking on any of the tabs, you can browse for information. Alternatively, you can type a keyword or several keywords into the 'Search' · A large number of copies of the latest print edition of the Directory are sent each year to all prisons and Approved Premises (APs) in the UK
🌐
Bulk Rename Utility
bulkrenameutility.co.uk › forum › viewtopic.php
Add (7) Word Space - Exclude Non-Alphanumeric Characters
Support for the Renaming Utility - www.bulkrenameutility.co.uk · The requested topic does not exist
🌐
Dyalog
help.dyalog.com › latest › Content › Language › Appendices › PCRE Regular Expression Details.htm
PCRE Regular Expression Details
If any other of these assertions ... sequence" error is gener- ated instead. A word boundary is a position in the subject string where the current character and the previous character do not both match \w or \W (i.e....