Python regex to match a specific word

python regex match word in string
python regex not
python regex tester
python regex cheat sheet
regex to extract words from string python
python regex multiple patterns
python split regex
multiline regex python

I want to match all lines in a test report, which contain words 'Not Ok'. Example line of text :

'Test result 1: Not Ok -31.08'

I tried this:

filter1 = re.compile("Not Ok")
for line in myfile:                                     
    if filter1.match(line): 
       print line

which should work according to, but I get nothing at the output. Any idea, what might be wrong? Tested various other parameters, like "." and "^Test" , which work perfectly.

You should use here not re.match.

From the docs on re.match:

If you want to locate a match anywhere in string, use search() instead.

If you're looking for the exact word 'Not Ok' then use \b word boundaries, otherwise if you're only looking for a substring 'Not Ok' then use simple : if 'Not Ok' in string.

>>> strs = 'Test result 1: Not Ok -31.08'
>>>'\bNot Ok\b',strs).group(0)
'Not Ok'
>>> match ='\bNot Ok\b',strs)
>>> if match:
...     print "Found"
... else:
...     print "Not Found"

How should I write a regex to match a specific word?, The word rocket will thus be in match group 1. UPDATE 1: Matt said in the comment that this regex is to be used in python. Python has a slightly different syntax. The regex should match ROCKET in upper or lower cases, and with or without punctuation, but not when part of another word. So, the regex would trigger on any of these: rocket RoCKEt hi Rocket This is a rocket.

You could simply use,

if <keyword> in str:
    print('Found keyword')


if 'Not Ok' in input_string:
    print('Found string')

Regular Expression HOWTO, For example, the regular expression test will match the string test exactly. Multiple flags can be specified by bitwise OR-ing them; re. A word is defined as a sequence of alphanumeric characters, so the end of a word is  You should use here not re.match.. From the docs on re.match:. If you want to locate a match anywhere in string, use search() instead. If you're looking for the exact word 'Not Ok' then use \b word boundaries, otherwise if you're only looking for a substring 'Not Ok' then use simple : if 'Not Ok' in string.

Absolutely no need to use RegEx in this case! Just use:

s = 'Test result 1: Not Ok -31.08'
if s.find('Not Ok') > 0 : 

or as already mentioned:

if 'Not Ok' in s:

Using Regex for Text Manipulation in Python, Matching String from the Start. To check if a string starts with a specific word, you can use the carrot key i.e. ^ followed by the word to match with  \b Matches the empty string, but only at the beginning or end of a word. Note that the re module uses a naive definition of "word" as a "sequence of alphanumeric or underscore characters", where "alphanumeric" depends on locale or unicode options. Also note that without the raw string prefix, \b is seen as "backspace" instead of regex word

Introduction to Regular Expressions in Python, A regular expression specifies a pattern that aims to match the input string. Regular expressions can also be used to filter certain words out of post comments,  I'm trying to use python's regular expression to match a string with several words. For example, the string is "These are oranges and apples and pears, but not pinapples or .." The list of words I want to find is 'and', 'or' and 'not'.

Python Regex: re.match(),, re.findall() with , In Python regular expression supports various things like Modifiers, In the example, we have split each word using the "re.split" function findall() module is used to search for “all” occurrences that match a given pattern. Pattern matching in Python with Regex Following regex is used in Python to match a string of three numbers, a hyphen, three more numbers, another hyphen, and four numbers. Regular expressions can be much more sophisticated. For example, adding a 3 in curly brackets ( {3}) after a pattern is like

How to match an exact word with Regex Python, You said “exact word”, and technically, the regex [code ]r'word'[/code] would get you that. However, I get the impression that might not be what you want,  In this tutorial, you will learn about regular expressions (RegEx), and use Python's re module to work with RegEx (with the help of examples). A Re gular Ex pression (RegEx) is a sequence of characters that defines a search pattern. The above code defines a RegEx pattern. The pattern is: any five letter string starting with a and ending with s.

  • Why regex if it's a specific string? Why not if "Not Ok" in line:?
  • BTW, re.match() only matches at the start of the string.
  • I was sure match could not only do the start of the string.. shouldn't $ match the end of line?
  • Maybe I was being unclear: re.match("a") is the same as"^a"). re.match("Not Ok") will return True for "Not Ok Joe!" and False for "It's Not Ok!".
  • ah, I see. Wasn't so clear from Python documentation. Learning something new every day!
  • indeed, this works, even without all extra \b stuff. Thanks! I also found that re.findall worked in this case.
  • @casper without \b it'll return True for something like this as well: 'KNot Oke'. re.findall returns a list of all non-overlapping matches, for checking purpose is the best choice.