 <p>This may help.</p> <pre><code>import re string = 'account_id 37318 not found' match = re.search(r'\baccount_id\b.*?\bnot found\b',string) if match: print 'Do something' else: print 'Do nothing' </code></pre> <p>Let me know if it helps :).</p>