I am trying to read movie names and years. The pattern is as follows:
name (year)
. The name of the movie can have all sorts of characters. And I came up with this pattern:
^(?P<name>.*) \((?P<year>\d*)\)
However, not all movies come with a year after them, so I would like make the year
group optional such that it returns blank if only the name of the movie is there.
I know I can make the year optional with ?
but then it ends up becoming part of the name
group.
CodePudding user response:
Your first part is too greedy. If you make the second part optional it will match everything. So, *
→ *?
Then you can use a optional non-capturing group for the year. And don't forget to mark the end:
^(?P<name>.*?)(?: \((?P<year>\d*)\))?$