Cut string to last name only

Hello everyone, I couldn't find anything about this topic in the forums so sorry if this is a double post. Looking for an optimization hint to improve my scripting skills..

I want to cut my %composer% field to cut all first names and the birth/death year and then write only the last name in the filename.

%composer% = Dr. Fanny Dammy-Joanne Sammy-Mendelssohn (1805-1847)
Resulting Filename = Sammy-Mendelssohn

I created a script that is really long (but working):
$cutleft($regexp(%composer%, (.),),$strrchr($regexp(%composer%, (.),), ))

(Of course, my script is only working if the last name has no spaces in it.)

Is there an EASIER way to do this?


What about this one:
$regexp(%composer%,.* (.) (.),$1)

It's funny, someone asked this last week on the Hydrogen Audio forums. Since foobar2000 doesn't support regular expression functions someone came up with the following, which finds the last word of a tag as separated by spaces (which is similar to your original function):

[$replace(%composer%,$left(%composer%,$strrchr(%composer%, )),)]

The second word from the right side ...

Formatstring: $regexp(%COMPOSER%,'.*?(\S+?)\s\S+$','$1') ... or ... Formatstring: $cutRight($cutLeft(%COMPOSER%,$strrchr($trim($left(%COMPOSER%,$strrchr(%COMPOSER%,' '))),' ')),$strchr($reverse(%COMPOSER%),' '))


Thank you all for your answers, I knew there had to be leaner solutions out there! :slight_smile:

@LaurenBacall: I would love to use a similiar function without regular expressions in foobar, however I need one that selects the second word from the right because I always include birth/death years in the composer tag...any ideas on that?

I tested the following in foobar2000 using your original composer example:

$puts(cmp-cut,$trim($left(%composer%,$strstr(%composer%,' ('))))
$puts(composer,$replace($get(cmp-cut),$left($get(cmp-cut),$strrchr($get(cmp-cut), )),))

It first cuts off everything after and including the parenthesis, then stores the result in a $puts() variable that is further trimmed and stored in another variable. The final result is returned with $get(), which can be used in further formatting. The variable names can be changed to anything.

Still relies on a last name that doesn't have spaces (though I struggle to think of any that do tbh), and that the composer name itself doesn't feature any brackets (apart from the end date that is).

Would be simpler to store the birth/death date in a custom tag and add it via formatting than the other way around, if you want the formatting lean in fb2k :wink:

Thanks! Indeed a good idea to work with custom tags...however for my mobile solution I am not using fb2k so unless fb2k mobile is released I need to keep that information in the composer tag. :confused: