Extract left text outside the parenthesis if any Im trying to extract the text on the left, outside the parenthesis ():

import re
clean_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:

&gt;&gt;'Alone in the Dark (The Cincinnati Series Book 2)'
&gt;&gt;'alone in the dark'
Assuming the text on the left cannot contain opening parenthesis, splitting by the first opening parenthesis might work as well:

&gt;&gt;&gt; clean_title = 'Alone in the Dark (The Cincinnati Series Book 2)'
&gt;&gt;&gt; clean_title.split('(', 1)[0].lower().rstrip()
'alone in the dark'


Note that it will not fail even if there are no parenthesis:

&gt;&gt;&gt; clean_title = 'Test'
&gt;&gt;&gt; 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:

&gt;&gt;&gt; re.sub(r"(.*?)\s?\(.*?\)", r"\1", clean_title).lower()
'alone in the dark'


Or:

&gt;&gt;&gt; re.sub(r"\s?\(.*?\)", r"", clean_title).lower()
'alone in the dark'


You can also do the lowering part in a replacement function:

&gt;&gt;&gt; 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.