Regex to extract movie name from file name

imagine if you run the following line:

"The Great Film (1999) DVDRip.avi"

I want to extract the string “Great movie” from this through regex, but I am struggling to get the correct regex for this.

I would use these overly syntactic file names of various lengths.

thanks!

+3
source share
3 answers

This syntax is designed around the .NET regexp analyzer (it may be different on other regex machines):

^(?<MovieName>.+)\((?<Year>\d+)\)(?<AdditionalText>[^\.]*)\.(?<Extension>[^\.]*)$

You can use this syntax to get the required data:

string line = "Movie Text";
Match match = Regex.Match(line);
match.Groups["MovieName"].Value;

You can also pull out a year, extra text and an extension if you need it.

+4
source

, :

^(.+)\s\(.+
+1

^([^(]+) 

(),

Python:

>>> import re
>>> re.compile("^([^(]+)").match("A Great Movie (1999) DVDRip.avi").groups()
('A Great Movie ',)
0

Source: https://habr.com/ru/post/1709255/


All Articles