I'm working on a program where I need to take user input for movies like "Interstellar (2014)" or "Interstellar 2014" and split them to get the movie title and release year. However, I'm stuck on how to incorporate movies like "Se7en" or "Lilo & Stitch" into my Regex. Here's the code I have so far:
```python
inputInfo = input("Enter Movie with year~# ")
regexRes = re.compile(r'((w+s)+)(d{4})')
regexParRes = re.compile(r'((w+s)+)((d{4}))')
if '(' in inputInfo:
info = re.search(regexParRes, inputInfo)
movie_name = info.group(1)
release_d = info.group(3)[1:-1]
else:
info = re.search(regexRes, inputInfo)
movie_name = info.group(1)
release_d = info.group(3)
```
Any advice on how to handle titles that feature special characters or different formatting?
4 Answers
You don't necessarily need pure regex for this. A good approach could be to use regex to extract the year and then simply replace it in the original string to get the movie name. This can streamline your code and make it more readable!
Alternatively, you could just ask the user for the movie name first, and then separately ask for the year. This way, you eliminate any issues with formatting altogether!
Here’s a regex pattern you might find useful:
```regex
^(.+)s+(?(d{4}))?$
```
This captures everything up to the last space and the four-digit year, which can be inside optional parentheses. This should work fine for multi-word titles or those that have numbers!
Instead of going full regex for this, you could simplify things by splitting the string on whitespace. Just take everything except the last part as the movie name, and grab the last part for the release year. This way, you won’t have to deal with complicated regex patterns every time!
Dude that's smart!
Hmm... there’s not really a need to stick to regex. I just wanted to use it since I'm learning about it for my project!