Extract year from directory name

I want assign to the %year% tag the first value found in %_directory% that starts with 19 or 20 and include the following two digits (to form the year). I have been trying to use the Format Value action.

$regexp(x,expr,repl)
replaces the pattern specified by the regular expression expr in the string x by repl. The
fourth optional parameter enables ignore case (1) or disables the ignore case setting (0).
Please note that you have to escape comma and other special characters in expr

My directories have names like:
1991 - Eric Clapton - 24 Nights
The Turtles - 20 Greatest Hits (1984)
1985 - George Benson - 20-20

Field=YEAR
Format string=$regexp(%_directory%,'(\b\d{4}\b)','$1') returns %_directory% (no change)

Format string=$regexp(%_directory%,'(\d{4})','$1') returns %_directory% (no change)

Format string=$regexp(%_directory%,'(19|20)\d{2}','$1') returns the original %_directory% string with the 2 chars after 19 or 20 stripped.

Format string=$regexp(%_directory%,'\b(19|20)\d{2}\b','$1') returns the original %_directory% string with the 2 chars after 19 or 20 stripped.

Format string=$regexp(%_directory%,\b\d{4}\b,'$1') returns everything but the year that I want! The year is stripped. I want exactly the opposite of this, I want the year as the result. This is a very simple regex and I don't know why it returns the opposite of what I want. If I remove the \b's the result is the same.

Result (opposite of what I want!):

  • Eric Clapton - 24 Nights
    The Turtles - 20 Greatest Hits () (FLAC+CUE)
  • George Benson - 20-20

I'm not sure how to do the parenthesis grouping and using the $1 stuff, may be the trick.
The below example should be perfect, but my results are the opposite of expected.
It has: 1. the stuff I want to ignore (not in parenthesis), 2. the stuff I want (in parenthesis), and 3. the stuff I don't want at the end (not in parenthesis).

I checked similar posts and one user had success with the regular expression: $regexp(%YEAR%,'^.(\d\d\d\d).$','$1')

I tried this and it gave me everything but the year (year had been stripped) and got result.

Try my examples. Do you get the same result? Can anyone figure this out?
It is very useful to grab years from the directory name.
Is is broken? This should be simple.
Please help!

Got it!
To extract just the year from the directory name:
Needed to add the ^.* at the beginning, before the match, and .*$ at the end after the match and have only the match part in parenthesis! The inner parenthesis are to select only years starting with 19 or 20, the outer set of parenthesis is the grouping for '$1'.

$regexp(%_directory%,'^.((19|20)\d{2}).$','$1') <= This gives 19xx or 20xx year
$regexp(%_directory%,'^.(\d\d\d\d).$','$1') <= This gives 4 digit year
$regexp(%_directory%,'^.(\d{4}).$','$1') <= This gives 4 digit year

Note for the commands above, when I viewed the post the asterisks are being dropped.
There should be carot dot asterisk before your (match) and dot asterisk dollar-sign at the end.

$regexp(%_directory%,'\b\d{4}\b','$1') <= Opposite: Directory name with year removed

Works for me now!

Even better result is to use %_folderpath% rather than %_directory% and when no match is found to blank the year.

Here's the improved formula: (you should see an asterisk after each of the dots, if you paste this formula and there are no asterisks, edit the formula and put them there).
$if($isdigit($regexp(%_folderpath%,'^.((19|20)\d{2}).$','$1')),$regexp(%_folderpath%,'^.((19|20)\d{2}).$','$1'),)

If you want to get text the way you typed it and no interpretation as formatting instruction then use the accent grave (top-left to bottom-right) to enclose that string, like string

Needed to add the ^.* at the beginning, before the match, and .*$ at the end

I used the 'Preformatted text' tool (</> button, or Ctrl+Shift+C) for that.