Im trying to extract the text on the left, outside the parenthesis ():import reclean_title = re.match(r'(.*)\(.*\)', title.lower()) if clean_title:   return clean_title.group(1).rstrip()Any suggestions to improve it, maybe without regex?Expected results:>>'Alone in the Dark (The Cincinnati Series Book 2)'>>'alone in the dark'
Extract left text outside the parenthesis if any
python;regex
Assuming the text on the left cannot contain opening parenthesis, splitting by the first opening parenthesis might work as well:>>> clean_title = 'Alone in the Dark (The Cincinnati Series Book 2)'>>> clean_title.split('(', 1)[0].lower().rstrip()'alone in the dark'Note that it will not fail even if there are no parenthesis:>>> clean_title = 'Test'>>> clean_title.split('(', 1)[0].lower().rstrip()'test'As far as the regular expression approach, you might also use re.sub() which would not require checking if there is a match or not. And, as a bonus, let's account for the extra space before the parenthesis:>>> re.sub(r(.*?)\s?\(.*?\), r\1, clean_title).lower()'alone in the dark'Or:>>> re.sub(r\s?\(.*?\), r, clean_title).lower()'alone in the dark'You can also do the lowering part in a replacement function:>>> re.sub(r(.*?)\s?\(.*?\), lambda m: m.group(1).lower(), clean_title)'alone in the dark'Also note that I'm using non-greedy regular expressions.