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
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"
.