>>> line1 = 'be nice'
>>> line2 = '"best!"'
>>> line3 = 'better?'
>>> line4 = 'oh no\nbear spotted'
>>> pat = re.compile(r'^be')
>>> bool(pat.search(line1))
True
>>> bool(pat.search(line2))
False
>>> bool(pat.search(line3))
True
>>> bool(pat.search(line4))
False
>>> words = 'bred red spread credible'
>>> re.sub(r'\bred','brown',words)
'bred brown spread credible'
>>> words = ['hi42bye', 'nice1423', 'bad42', 'cool_42a', 'fake4b']
>>> [w for w in words if re.search(r'42',w)]
['hi42bye', 'nice1423', 'bad42', 'cool_42a']
>>> items = ['lovely', '1\ndentist', '2 lonely', 'eden', 'fly\n', 'dent']
>>> [e for e in items if re.search(r'^den',e) or re.search(r'ly\Z',e)]
['lovely', '2 lonely', 'dent']
>>> para = '''\
... ball fall wall tall
... mall call ball pall
... wall mall ball fall
... mallet wallet malls'''
>>> print(re.sub(r'^mall\b','1234',para,flags=re.M))
ball fall wall tall
1234 call ball pall
wall mall ball fall
mallet wallet malls
>>> items = ['12\nthree\n', '12\nThree', '12\nthree\n4', '12\nthree']
>>> [w for w in items if re.search(r'^12\nthree\Z',w,flags=re.I)]
['12\nThree', '12\nthree']
>>> items = ['handed', 'hand', 'handy', 'unhanded', 'handle', 'hand-2']
>>> [re.sub(r'^hand','X',w) for w in items]
['Xed', 'X', 'Xy', 'unhanded', 'Xle', 'X-2']
>>> items = ['handed', 'hand', 'handy', 'unhanded', 'handle', 'hand-2']
>>> [re.sub(r'e','X',w) for w in items if re.search(r'^hand',w)]
['handXd', 'hand', 'handy', 'handlX', 'hand-2']