Expression Needed to Retain One Word in a Sentence

Good day, Everybody :smiley:

Could you please help me out with an regular expression (or two), which will, in effect:

  1. Retain the last word only, in a sentence in a field.

  2. Retain the first word only, in a sentence in a field.

Thank you.

first word: $regexp(%TITLE%,'^(\w+)\b.*$','$1') last word: $regexp(%TITLE%,'^.*\b(\w+)$','$1')


Thank you, DetlevD, that was QUICK. :slight_smile:

Is there perhaps a way to do this using the TRIM-script too?

Please show us the content from the tagfield ALBUMARTIST in question.


Sorry, my error, and I fixed it: I erred by going straight for a regular expression action. Now created a format value action with your regex, and all is well now. :slight_smile: I revised my earlier post.

For interest sake, I would still like to know if the same result can be achieved using the trim-script?

Thank you very much again.

Note: Within Mp3tag there is no "trim-script".

Do open ...
... and read how the functions $trim, $trimLeft, $trimRight do work.


OK, got it. Thank you.

A problem arose: How should I adapt the two given expressions to retain hyphenated words as one?

Example "Saint-Saens".

first word can have hyphen: $regexp(%TITLE%,'^([\w-]+)\s.*$','$1') last word can have hyphen: $regexp(%TITLE%,'^.*\s([\w-]+)$','$1')


Thank you DetlevD. I appreciate your help very much indeed.

First off thankyou for this it has eased my workflow immensely. It resolved months of banging head on desk and having to have multiple scripts to do the same thing for slight variants.

My question here is this can this script be modified slightly to include a character that is at the end of a word but is not an actual word character. Case in point, I have ORKUS! as a publisher/label with a number of volumes and best of compilations underneath it now using the 1st word option, I get the ORKUS (Minus the !)..would like to include the ! but when I try to apply the \W to replace the \w that you are using which works perfectly it goes awry? Any suggestions?

$regexp('aaa bbb-ccc!','^.*\s([\w!-]+)$','$1') ==> 'bbb-ccc!'


Well that is simple must have been overthinking it .... which is why I read and reread the forums to see what can be useful...

Thanks as DetlevD