if regex in string python 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: Python Regex documentation\
>>> import re
>>> m = re.search('(?<=abc)def', 'abcdef')
>>> m.group(0)
'def'