tags:

views:

129

answers:

3

Hi Guys,

imagine if you will the following string:

"A Great Movie (1999) DVDRip.avi"

I am looking to extract the string "A Great Movie" from this via regex, but struggling to get the correct regex for this.

I would be using this too parse file names of various length.

thanks!

+1  A: 

Assuming the bracket is preceded by a space:

^(.+)\s\(.+
benPearce
+2  A: 

This syntax is designed around the .NET regex parser (may be different in other regex engines):

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

You can use this syntax to get out the data you want:

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

You can also pull out the Year, AdditionalText, and Extension if you need it.

This is perfect for me. You have just saved me a lot of work! Thanks!
A: 

Matches everything up to the first bracket

^([^(]+)

Faster (marginally) than Ben's option

Python:

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