Adding Parenthesis'

A swapping-ground for Regular Expression syntax

Adding Parenthesis'

Postby jets » Mon Jan 19, 2009 6:19 pm

I have no idea how to go about doing this but I have countless movies and all I'd like to do is add a left/right parenthesis for the year but specify that the number/year has to be 4 numerals long. The year is not always at the end of the filename either. I'd like the end result to be "MovieTitle (2009)" instead of "MovieTitle 2009"

Any help you can provide would be appriciated. Thanks!
jets
 
Posts: 2
Joined: Mon Jan 19, 2009 6:11 pm

Re: Adding Parenthesis'

Postby GMA » Tue Jan 20, 2009 9:12 pm

Hi, jets:
Try using the following in the "RegEx(1)" field:

MATCH: (.*)([0-9]{4})(.*)
REPLACE: \1(\2)\3


It'll work with almost every filename, except where there's a 4 digit number also in the movie title (e.g. "2001, A Space Odyssey"). In those special cases you might want to confirm that you're getting the desired result.
For example: 2001, A Space Odyssey_1968.avi will be renamed as 2001, A Space Odyssey_(1968).avi, and that's correct.
But if the file name is 1968_2001, A Space Odyssey.avi, after renaming it'll be 1968_(2001), A Space Odyssey.avi, so then you're gonna need to manually change it to (1968)_2001, A Space Odyssey.avi.
Anyway, you might not even have those special cases among your files, since they're not so common.
Best regards,

Gabriel.
GMA
 
Posts: 91
Joined: Sun Dec 02, 2007 1:30 pm
Location: Argentina

Re: Adding Parenthesis'

Postby jets » Tue Jan 20, 2009 9:38 pm

THANK YOU!
jets
 
Posts: 2
Joined: Mon Jan 19, 2009 6:11 pm

Re: Adding Parenthesis'

Postby Brainsan » Sat Jan 11, 2014 3:22 pm

Thank you! This almost solved my challenge, and it did with one tweak. My case was complicated by this format: "MovieTitle 2009 1080p", and the regex was matching 1080 instead of the year.

Now I know nothing about regex, and there's probably a better way to do it, but this works:

Change:
MATCH: (.*)([0-9]{4})(.*)
to:
MATCH: (.*)([0-9]{4}) (.*)

The space between the last two pairs of parens breaks the match with 1080p.
Brainsan
 
Posts: 1
Joined: Sat Jan 11, 2014 3:15 pm

Re: Adding Parenthesis'

Postby Stefan » Sat Jan 11, 2014 7:57 pm

Brainsan wrote:Thank you! This almost solved my challenge, and it did with one tweak.
My case was complicated by this format: "MovieTitle 2009 1080p", and the regex was matching 1080 instead of the year.

Now I know nothing about regex, and there's probably a better way to do it, but this works:

Change:
MATCH: (.*)([0-9]{4})(.*)
to:
MATCH: (.*)([0-9]{4}) (.*)

The space between the last two pairs of parens breaks the match with 1080p.



Well done Brain.

For better understanding:
. will match on one piece of any kind
* will match zero-or-more of the previous expression ( . in our case) [if you want to be save, use + instead, which will much one-at-least-and-more]
[0-9] or \d will match one piece of an digit
{4} is like the star an multiplier and will match exactly 4 pieces of the expression right before (one digit in our case)

Now, since this regex engine works greedy, ".*" will match everything till it finds 4 digits (\d{4}), the "1080".

You have solved that by telling the regex engine to match four digits followed by an space, and since that is the case for the year but not for 1080p, you hit it :D

BTW; another solution could be to make the .* expression non-greedy by adding a question-mark ?
MATCH: (.*?)([0-9]{4})(.*)

Now the engine stops once it finds the first four digits. But here the last backreferencing group will capture the leading space too.
As always it depends on your needs if this is fine for you or break the job.


Examples on "MovieTitle 2009 1080p"
MATCH: (.*)([0-9]{4})(.*) >>>  \1 = "MovieTitle 2009 " ; \2 = "1080" ;     \3 = "p"

Find first digits by matching an following space:
MATCH: (.*)([0-9]{4}) (.*) >>> \1 = "MovieTitle "         ; \2 = "2009" ;     \3 = "1080p"

Find first digits by letting first expression matching lazy only:
MATCH: (.*?)([0-9]{4})(.*) >>> \1 = "MovieTitle "         ; \2 = "2009" ;     \3 = " 1080p"


.
Stefan
 
Posts: 736
Joined: Fri Mar 11, 2005 7:46 pm
Location: Germany, EU


Return to Regular Expressions