re.match matches at the beginning of the input string.

To match anywhere, use re.search instead.

>>> import re
>>> re.match('a', 'abc')
<_sre.SRE_Match object at 0x0000000001E18578>
>>> re.match('a', 'bac')
>>> re.search('a', 'bac')
<_sre.SRE_Match object at 0x0000000002654370>

See search() vs. match():

Python offers two different primitive operations based on regular expressions: re.match() checks for a match only at the beginning of the string, while re.search() checks for a match anywhere in the string (this is what Perl does by default).

Answer from falsetru on Stack Overflow
๐ŸŒ
Quora
quora.com โ€บ How-do-you-match-an-exact-word-with-Regex-Python
How to match an exact word with Regex Python - Quora
\b matches between a word character (\w: [A-Za-z0-9_]) and a non-word character; words with punctuation or Unicode letters may need special handling. For Unicode-aware boundaries, Python's re is Unicode-aware by default, but \w doesn't include all language-specific letters in some edge cases; consider the regex module (third-party) for advanced Unicode classes. Use re.escape when inserting user-controlled strings ...
Discussions

Exact Word Match in String with Regex in Python - Post.Byes
Hello, I've been trying to come up with a way to search for the exact match of one word (as a string) within another string. So, for example, if I were searching for the string, 'eligible', searching it against the string 'ineligible' would not yield a result. More on post.bytes.com
๐ŸŒ post.bytes.com
Regex exact match search
https://regex101.com/r/CCCB3i/1 ^-means start of string $-means the end of string More on reddit.com
๐ŸŒ r/regex
3
3
March 11, 2022
How to match exact word with regex python? - Stack Overflow
I am trying to match exact words with regex but it's not working as I expect it to be. Here's a small example code and data on which I'm trying this. I am trying to match c and java words in a string if found then return true. More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - How to match an exact word inside a string? - Stack Overflow
op = ['TRAIL_RATE_ID 8 TRAIL_RATE_NAME VC-4 TRAIL_ORDER High Order ', 'TRAIL_RATE_ID 9 TRAIL_RATE_NAME VC4-4 TRAIL_ORDER High Order ' , 'TRAIL_RATE_ID 10 TRAIL_RATE_NAME VC-8 TRAIL_ORDER High Or... More on stackoverflow.com
๐ŸŒ stackoverflow.com
July 30, 2016
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ how to match an exact word in python regex? (answer: donโ€™t)
How to Match an Exact Word in Python Regex? (Answer: Don't) - Be on the Right Side of Change
May 31, 2022 - So if youโ€™re an impatient person, hereโ€™s the short answer: To match an exact string 'hello' partially in 'hello world', use the simple regex 'hello'. However, a simpler and more Pythonic approach would be using the in keyword within membership expression 'hello' in 'hello world'.
๐ŸŒ
AskPython
askpython.com โ€บ python โ€บ examples โ€บ matching-strings-using-regular-expressions
Matching Entire Strings in Python using Regular Expressions - AskPython
February 27, 2023 - Open a command prompt or terminal ... specified regular expression pattern. To match an exact string, you can simply pass the string as the pattern....
๐ŸŒ
YouTube
youtube.com โ€บ finxter
How to Match an Exact Word in Python Regex? (Answer: Donโ€™t) - YouTube
This morning, I read over an actual Quora thread with this precise question. While thereโ€™s no dumb question, the question reveals that there may be some gap ...
Published ย  March 4, 2020
Views ย  2K
๐ŸŒ
Bytes
bytes.com โ€บ home โ€บ forum โ€บ topic โ€บ python
Exact Word Match in String with Regex in Python - Post.Byes
March 13, 2013 - Hello, I've been trying to come up with a way to search for the exact match of one word (as a string) within another string. So, for example, if I were searching for the string, 'eligible', searching it against the string 'ineligible' would not yield a result. Here is the code I have thus far: ... def findstring(string1, string2): if re.search(r'(^|[^a-z0-9])'+re.escape(string1)+r'($|[^a-z0-9])', string2): return True return False This, however, still would match 'ineligible.' What is the regex needed to ensure that only exact words are matched?
๐ŸŒ
ZetCode
zetcode.com โ€บ python โ€บ regularexpressions
Python regular expressions - using regular expressions in Python
The fullmatch function looks an exact match. ... #!/usr/bin/python import re words = ('book', 'bookworm', 'Bible', 'bookish','cookbook', 'bookstore', 'pocketbook') pattern = re.compile(r'book') for word in words: if re.fullmatch(pattern, word): print(f'The {word} matches')
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/regex โ€บ regex exact match search
r/regex on Reddit: Regex exact match search
March 11, 2022 -

I'm searching for exact word match of the word - "new"
in the string - "This is a new String"
using the regex - /^new$/
But the regex101 does not match anything, what am i missing ?
https://regex101.com/r/o1TBGn/1

๐ŸŒ
sqlpey
sqlpey.com โ€บ python โ€บ python-regex-exact-words
Python Regex: Locate Exact Words in Strings - sqlpey
July 22, 2025 - Explanation: The re.search(r'\bis\b', your_string) looks for the sequence โ€œisโ€ (is) only when itโ€™s surrounded by word boundaries (\b). This prevents matches within words like โ€œthisโ€ or โ€œthickness.โ€ Itโ€™s crucial to use a raw string (r'...') for the regex pattern to ensure backslashes are treated literally, otherwise \b would be interpreted as a backspace character.
๐ŸŒ
Predictivehacks
predictivehacks.com
Regular Expression for Exact Match โ€“ Predictive Hacks
keyword = "$ 30" keyword = re.escape(keyword) df.loc[df.Document.str.contains(rf"(?<!\w){keyword}(?!\w)", case=False, regex=True)]
Top answer
1 of 2
2

You may consider the following approaches.

TLS as a whole word should have a word boundary right in front of it, so that part is covered in your pattern.

If there must be a whitespace right after 1, or end of string, it is more efficient to use a negative lookahead (?!\S): r'\bTLS 1(?!\S)'. Well, you may also use r'\bTLS 1(?:\s|$)'. See this regex demo.

If you just want to ensure there is no digit or a fractional part after 1 use

r'\bTLS 1(?!\.?\d)'

This will match TLS 1 that has no . or . + digit after it. See this regex demo.

Python demo:

import re
target = ['TLS 1.2 x67 DHE-RSA-AES128-SHA256 DH 2048 AES128 TLS_DHE_RSA_WITH_AES_128_CBC_SHA256', 'TLS 1 x67 DHE-RSA-AES128-SHA256 DH 2048 AES128 TLS_DHE_RSA_WITH_AES_128_CBC_SHA256', 
'TLS 1.1 x67 DHE-RSA-AES128-SHA256 DH 2048 AES128 TLS_DHE_RSA_WITH_AES_128_CBC_SHA256']
lines=[]
for i in target:
    if re.match(r'\bTLS 1(?!\.?\d)', i):
        lines.append(i)
print(lines)

Output:

['TLS 1 x67 DHE-RSA-AES128-SHA256 DH 2048 AES128 TLS_DHE_RSA_WITH_AES_128_CBC_SHA256']
2 of 2
2

Wiktor commented before I posted this (not surprising), but the marker for an exact match in this case is actually a space following TLS 1. A word boundary is not specific enough, because that would also pick up things like TLS 1.1, which you don't want. So try this version:

#returns all the lines including TLS 1.1, TLS 1.2 ...    
lines = []    
    for i in target:
        if re.match(r'\bTLS 1\s', i):
            lines.append(i)

If the TLS text could possibly be the very last thing in a line, then we can try using this:

re.match(r'\bTLS 1(?=(\s|$))', i)
Top answer
1 of 2
7

Make your own word-boundary:

def exact_Match(phrase, word):
    b = r'(\s|^|$)' 
    res = re.match(b + word + b, phrase, flags=re.IGNORECASE)
    return bool(res)

copy-paste from here to my interpreter:

>>> str1 = "award-winning blueberries"
>>> word1 = "award"
>>> word2 = "award-winning"
>>> exact_Match(str1, word1)
False
>>> exact_Match(str1, word2)
True

Actually, the casting to bool is unnecessary and not helping at all. The function is better off without it:

def exact_Match(phrase, word):
    b = r'(\s|^|$)' 
    return re.match(b + word + b, phrase, flags=re.IGNORECASE)

note: exact_Match is pretty unconventional casing. just call it exact_match.

2 of 2
2

The problem with your initial method is that '\\b' does not denote the zero-width assertion search that your looking for. (And if it did, I would use r'\b' instead because backslashes can become a real hassle in regular expressions - see this link)

From Regular Expression HOWTO

\b

Word boundary. This is a zero-width assertion that matches only at the beginning or end of a word. A word is defined as a sequence of alphanumeric characters, so the end of a word is indicated by whitespace or a non-alphanumeric character.

Because - is a non-alphanumeric character, your findall regular expression will find award in award-wining but not in awards.

Depending on your searched phrase, I would also think of using re.findall instead of re.match as suggested by Elazar. In your example re.match works, but if the word you are looking for is nested anywhere beyond the beginning of the string, re.match will not succeed.

๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-match-a-word-in-python-using-regular-expression
How to match a word in python using Regular Expression?
June 8, 2025 - In Python, the re.search() method is used to search for a pattern within a given string. If the pattern is found, then it returns a match object otherwise, it returns None. To match a specific word, we can use the pattern with \b, which indicates a word boundary.