python regex check if string contains code example
Example 1: check if valid regex string python
import re
string = '['
try:
re.compile(string)
is_valid = True
except re.error:
is_valid = False
Example 2: re contains
fullstring = "StackAbuse"
substring = "tack"
if substring in fullstring:
print "Found!"
else:
print "Not found!"
Example 3: check if string contains python
>>> str = "Messi is the best soccer player"
>>> "soccer" in str
True
>>> "football" in str
False