Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python regex to get everything until an expression like ''(year)"

Tags:

python

regex

I have a data frame column named 'movie_title' which has movie names along with year. Following are two types of movie titles in the above mentioned column.

title1='Toy Story (1995)'
title2='City of Lost Children, The (Cité des enfants perdus, La) (1995)'

I want to split this into two columns with title and release year. I was able to extract years successfully using following regex:

re.findall('[1-2][0-9]{3}', string)[0]

Need help in writing another regex which can extract titles(excluding year info along with brackets).

e.g. title1 and title2 should look like:

title1='Toy Story'
title2='City of Lost Children, The (Cité des enfants perdus, La)'
like image 801
shashank kumar Avatar asked Aug 03 '26 04:08

shashank kumar


1 Answers

This does the trick almost:

.(?:[^\((0-9)])+

You just need to get rid of the trailing ) that it doesn't capture. Will update this answer if I find anything better.

Another thought: If you are sure that the year will appear at the end of every movie title, why not just strip the last bit off? So remove (xxxx) off of every movie string you have?

like image 140
peachykeen Avatar answered Aug 05 '26 17:08

peachykeen