In the Perl era I was a regex freak. I definitely struggle adapting to re. To simplify a big data set I needed to search a "|" character and the only combination that would work was re.escape'|'
and re.search
instead of re.match
import re
x = re.compile((re.escape'|'))
cohort = ['virus_1', 'virus_2|virus_3']
for isolate in cohort:
# note that re.escape(isolate) fails
if x.search(isolate):
print(isolate)
OUTPUT
virus_2|virus_3
Okay the above combination works, but re.match
doesn't work. Also why do I need re.escape('|') and why does re.escape(isolate), i.e. the list element, fail? What am I missing to routinely use re?