To get the regex match a word in the string use re.search here not re.match method in Python.
From the docs on
Python regex matches a word in a string example
Simple example code If you want to locate a match anywhere in the string, use search() instead.
If the search is for the exact word ‘Not Ok’ then use \b word boundaries, otherwise only looking for a substring ‘Not Ok’ then use simply:
if 'Not Ok' in string.
For this example, you have to import the re module.
import re strs = 'Test result 1: Not Ok -31.08' res = re.search(r'\bNot Ok\b', strs).group(0) print(res)
Using the if condition with the same example
import re strs = 'Test result 1: Not Ok -31.08' match = re.search(r'\bNot Ok\b', strs) if match: print("Found") else: print("Not Found")
Do comment if you have any doubts or suggestions on this Python regex topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.