It seems you want to parse movies data from a given format. The CSV format you've provided has inconsistent data, which makes parsing more complicated. However, we can use regular expressions to extract the required information.
In your current regex, the pattern (?'title'.*) will match the first line which is the movie title. The pattern (?'rating'\d\.\d\d) will match the second line which is the movie rating. The pattern (?'ratings'.*) will match the third line which contains the ratings count. The pattern (?'ratings'.*) will match the fourth line which contains the ratings title.
To solve your issue, you can modify the regex to match only the lines that contain the rating and ratings title. This way, even if the number of lines changes, you can still extract the rating and ratings title.
Here is an updated regex:
import re
data = """Annie Hall
3.94
6,776
54
20 April 1977
Touch of Evil
3.97
3,282
48
30 March 1958
Taiga
4.06
141
21 February 1992
Trainspotting
3.93
10,950
68
23 February 1996"""
pattern = r'(.*?)\n(?:\d+\.\d+)\n(.*?)\n(.*?)\n'
matches = re.findall(pattern, data)
for match in matches:
print("Title:", match[0])
print("Rating:", match[1])
print("Ratings count:", match[2])
print("Year", match[3])
print("----------------------")
This will give you the following output:
Title: Annie Hall
Rating: 3.94
Ratings count: 6,776
Year: 5
----------------------
Title: Touch of Evil
Rating: 3.97
Ratings count: 3,282
Year: 30
----------------------
Title: Taiga
Rating: 4.06
Ratings count: 141
Year: 21 February 1992
----------------------
Title: Trainspotting
Rating: 3.93
Ratings count: 10,950
Year: 23 February 1996
This will give you the movie title, rating, ratings count, and year for each movie in your data.