# Expression Needed to Retain One Word in a Sentence

**URL:** <https://community.mp3tag.de/t/expression-needed-to-retain-one-word-in-a-sentence/19445>\
**Category:** Support\
**Created:** [October 16, 2017, 6:50am UTC](https://community.mp3tag.de/t/expression-needed-to-retain-one-word-in-a-sentence/19445 "2017-10-16T06:50:16Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![ragtag](https://community.mp3tag.de/user_avatar/community.mp3tag.de/ragtag/32/13146_2.png) [@ragtag](https://community.mp3tag.de/u/ragtag)\
**Post date:** [October 16, 2017, 6:50am UTC](https://community.mp3tag.de/t/expression-needed-to-retain-one-word-in-a-sentence/19445/1 "2017-10-16T06:50:16Z")

</div>

Good day, Everybody 😃

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.

---

<div class="post-metadata">

**Author:** ![DetlevD](https://community.mp3tag.de/user_avatar/community.mp3tag.de/detlevd/32/123_2.png) [@DetlevD](https://community.mp3tag.de/u/DetlevD)\
**Post date:** [October 16, 2017, 7:02am UTC](https://community.mp3tag.de/t/expression-needed-to-retain-one-word-in-a-sentence/19445/2 "2017-10-16T07:02:43Z")

</div>

> [@ragtag](#):
>
> ... 1. Retain the **last word only** , in a ... field.  
> 2. Retain the **first word only** , in a ... field. ...

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

DD.20171016.1102.CEST

---

<div class="post-metadata">

**Author:** ![ragtag](https://community.mp3tag.de/user_avatar/community.mp3tag.de/ragtag/32/13146_2.png) [@ragtag](https://community.mp3tag.de/u/ragtag)\
**Post date:** [October 16, 2017, 7:17am UTC](https://community.mp3tag.de/t/expression-needed-to-retain-one-word-in-a-sentence/19445/3 "2017-10-16T07:17:42Z")

</div>

> [@DetlevD](#):
>
> **first word: $regexp(%TITLE%,'^(\w+)\b.\*$','$1') last word: $regexp(%TITLE%,'^.\*\b(\w+)$','$1')**
> 
> DD.20171016.1102.CEST

Thank you, DetlevD, that was QUICK. 🙂

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

---

<div class="post-metadata">

**Author:** ![DetlevD](https://community.mp3tag.de/user_avatar/community.mp3tag.de/detlevd/32/123_2.png) [@DetlevD](https://community.mp3tag.de/u/DetlevD)\
**Post date:** [October 16, 2017, 7:28am UTC](https://community.mp3tag.de/t/expression-needed-to-retain-one-word-in-a-sentence/19445/4 "2017-10-16T07:28:39Z")

</div>

> [@ragtag](#):
>
> ... Tried both with %AlbumArtist%, but no change? ...

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

DD.20171016.1128.CEST

---

<div class="post-metadata">

**Author:** ![ragtag](https://community.mp3tag.de/user_avatar/community.mp3tag.de/ragtag/32/13146_2.png) [@ragtag](https://community.mp3tag.de/u/ragtag)\
**Post date:** [October 16, 2017, 7:48am UTC](https://community.mp3tag.de/t/expression-needed-to-retain-one-word-in-a-sentence/19445/5 "2017-10-16T07:48:33Z")

</div>

> [@DetlevD](#):
>
> Please show us the content from the tagfield ALBUMARTIST in question.
> 
> DD.20171016.1128.CEST

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

---

<div class="post-metadata">

**Author:** ![DetlevD](https://community.mp3tag.de/user_avatar/community.mp3tag.de/detlevd/32/123_2.png) [@DetlevD](https://community.mp3tag.de/u/DetlevD)\
**Post date:** [October 16, 2017, 8:06am UTC](https://community.mp3tag.de/t/expression-needed-to-retain-one-word-in-a-sentence/19445/6 "2017-10-16T08:06:02Z")

</div>

> [@ragtag](#):
>
> ... I would still like to know if the same result can be achieved using the trim-script? ...

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

Do open ...  
[https://docs.mp3tag.de/scripting](https://docs.mp3tag.de/scripting)  
... and read how the functions $trim, $trimLeft, $trimRight do work.

DD.20171016.1205.CEST

---

<div class="post-metadata">

**Author:** ![ragtag](https://community.mp3tag.de/user_avatar/community.mp3tag.de/ragtag/32/13146_2.png) [@ragtag](https://community.mp3tag.de/u/ragtag)\
**Post date:** [October 16, 2017, 8:31am UTC](https://community.mp3tag.de/t/expression-needed-to-retain-one-word-in-a-sentence/19445/7 "2017-10-16T08:31:33Z")

</div>

OK, got it. Thank you.

---

<div class="post-metadata">

**Author:** ![ragtag](https://community.mp3tag.de/user_avatar/community.mp3tag.de/ragtag/32/13146_2.png) [@ragtag](https://community.mp3tag.de/u/ragtag)\
**Post date:** [October 16, 2017, 9:56am UTC](https://community.mp3tag.de/t/expression-needed-to-retain-one-word-in-a-sentence/19445/8 "2017-10-16T09:56:48Z")

</div>

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

Example "Saint-Saens".

---

<div class="post-metadata">

**Author:** ![DetlevD](https://community.mp3tag.de/user_avatar/community.mp3tag.de/detlevd/32/123_2.png) [@DetlevD](https://community.mp3tag.de/u/DetlevD)\
**Post date:** [October 16, 2017, 3:25pm UTC](https://community.mp3tag.de/t/expression-needed-to-retain-one-word-in-a-sentence/19445/9 "2017-10-16T15:25:08Z")

</div>

> [@ragtag](#):
>
> 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')**

DD.20171016.2000.CEST

---

<div class="post-metadata">

**Author:** ![ragtag](https://community.mp3tag.de/user_avatar/community.mp3tag.de/ragtag/32/13146_2.png) [@ragtag](https://community.mp3tag.de/u/ragtag)\
**Post date:** [October 16, 2017, 4:14pm UTC](https://community.mp3tag.de/t/expression-needed-to-retain-one-word-in-a-sentence/19445/10 "2017-10-16T16:14:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ron\_Mark](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/r/c37758/32.png) [@Ron\_Mark](https://community.mp3tag.de/u/Ron_Mark)\
**Post date:** [November 2, 2017, 5:43pm UTC](https://community.mp3tag.de/t/expression-needed-to-retain-one-word-in-a-sentence/19445/11 "2017-11-02T17:43:18Z")

</div>

> [@DetlevD](#):
>
> **first word can have hyphen: $regexp(%TITLE%,'^([\w-]+)\s.\*$','$1') last word can have hyphen: $regexp(%TITLE%,'^.\*\s([\w-]+)$','$1')**
> 
> DD.20171016.2000.CEST

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?

---

<div class="post-metadata">

**Author:** ![DetlevD](https://community.mp3tag.de/user_avatar/community.mp3tag.de/detlevd/32/123_2.png) [@DetlevD](https://community.mp3tag.de/u/DetlevD)\
**Post date:** [November 2, 2017, 7:36pm UTC](https://community.mp3tag.de/t/expression-needed-to-retain-one-word-in-a-sentence/19445/12 "2017-11-02T19:36:39Z")

</div>

> [@MyAlteredEgo](#):
>
> ... 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! ...

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

DD.20171102.2136.CET

---

<div class="post-metadata">

**Author:** ![Ron\_Mark](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/r/c37758/32.png) [@Ron\_Mark](https://community.mp3tag.de/u/Ron_Mark)\
**Post date:** [November 2, 2017, 8:45pm UTC](https://community.mp3tag.de/t/expression-needed-to-retain-one-word-in-a-sentence/19445/13 "2017-11-02T20:45:21Z")

</div>

> [@DetlevD](#):
>
> $regexp('aaa bbb-ccc!','^.\*\s([\w!-]+)$','$1') ==\> 'bbb-ccc!'
> 
> DD.20171102.2136.CET

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 always...to DetlevD

---

<div class="post-metadata">

**Author:** ![system](https://community.mp3tag.de/uploads/default/original/2X/c/ce7035d426cb755a7916793326d23b465222a407.png) [@system](https://community.mp3tag.de/u/system)\
**Post date:** [February 9, 2026, 12:27pm UTC](https://community.mp3tag.de/t/expression-needed-to-retain-one-word-in-a-sentence/19445/14 "2026-02-09T12:27:03Z")

</div>


