0

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.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563

1 Answers1

0

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*)\))?$

https://regex101.com/r/zTZd9a/2

Klaus D.
  • 13,874
  • 5
  • 41
  • 48