# Remove leading and trailing spaces from all fields

**URL:** <https://community.mp3tag.de/t/remove-leading-and-trailing-spaces-from-all-fields/15606>\
**Category:** Support\
**Created:** [March 22, 2014, 9:16pm UTC](https://community.mp3tag.de/t/remove-leading-and-trailing-spaces-from-all-fields/15606 "2014-03-22T21:16:58Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisjj](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@chrisjj](https://community.mp3tag.de/u/chrisjj)\
**Post date:** [March 22, 2014, 9:16pm UTC](https://community.mp3tag.de/t/remove-leading-and-trailing-spaces-from-all-fields/15606/1 "2014-03-22T21:16:58Z")

</div>

Is there a way to do this in a way that works regardless of what fields are in the tracks?

Action "Format value" would require a copy for each field name.

---

<div class="post-metadata">

**Author:** ![chrishirst](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/c/4af34b/32.png) [@chrishirst](https://community.mp3tag.de/u/chrishirst)\
**Post date:** [March 22, 2014, 9:48pm UTC](https://community.mp3tag.de/t/remove-leading-and-trailing-spaces-from-all-fields/15606/2 "2014-03-22T21:48:36Z")

</div>

Regexp replace

Field: \_ALL

pattern: ^[\s]+|[\s]+$

Replace With:

---

<div class="post-metadata">

**Author:** ![chrisjj](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@chrisjj](https://community.mp3tag.de/u/chrisjj)\
**Post date:** [March 22, 2014, 10:34pm UTC](https://community.mp3tag.de/t/remove-leading-and-trailing-spaces-from-all-fields/15606/3 "2014-03-22T22:34:59Z")

</div>

That works great. Thanks.

> [@Chris Hirst](#):
>
> pattern: **\[1\]+|[\s]+$**

Does that have some advantage over pattern: **^ +| +$**?

* * *

1. \s

---

<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:** [March 23, 2014, 6:07am UTC](https://community.mp3tag.de/t/remove-leading-and-trailing-spaces-from-all-fields/15606/4 "2014-03-23T06:07:12Z")

</div>

> [@Chris Hirst](#):
>
> ... Field: \_ALL ...

Although the pseudo tag-name \_ALL is introduced as a superset combination of \_TAG and \_FILENAME, the action "Replace using Regular Expression" cannot replace trailing spaces in the filename.  
The same result is valid for the pseudo tag-name \_FILENAME alone.

This special effect has been introduced due to the fact that the action "Replace using Regular Expression" is the only Mp3tag tool to change the file name extension.

Trailing spaces can be removed from the filename by respecting and saving the filename extension using the action "Replace using Regular Expression" ...  
Field: \_FILENAME  
Expression: \s+(..\*$)  
Replacement: $1

Trailing spaces can be removed from the filename by ...  
Convert "Tag - Tag"  
Action "Format value"  
Field: \_FILENAME  
Formatstring: $regexp(%\_filename%,'\s+$',)

DD.20140323.0819.CET

---

<div class="post-metadata">

**Author:** ![chrishirst](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/c/4af34b/32.png) [@chrishirst](https://community.mp3tag.de/u/chrishirst)\
**Post date:** [March 23, 2014, 9:09am UTC](https://community.mp3tag.de/t/remove-leading-and-trailing-spaces-from-all-fields/15606/5 "2014-03-23T09:09:34Z")

</div>

> [@chrisjj](#):
>
> That works great. Thanks.
> 
> Does that have some advantage over pattern: **^ +| +$**?

Readability for one, you have to be careful with putting spaces in a regex, so using /s in a character class makes it obvious that it is intended to match a space, also \s is an abbreviation of the POSIX [:space:] class, and will match ANY white space characters, such as ASCII codes VT, TAB, FF etc. whereas a literal space only matches a space.

---

<div class="post-metadata">

**Author:** ![chrisjj](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@chrisjj](https://community.mp3tag.de/u/chrisjj)\
**Post date:** [March 24, 2014, 11:40am UTC](https://community.mp3tag.de/t/remove-leading-and-trailing-spaces-from-all-fields/15606/6 "2014-03-24T11:40:21Z")

</div>

> [@DetlevD](#):
>
> Although the pseudo tag-name \_ALL is introduced as a superset combination of \_TAG and \_FILENAME, the action "Replace using Regular Expression" cannot replace trailing spaces in the filename.

The stated requirement is to remove leading and trailing spaces from all **fields** and by implication nothing else e.g. filename. Though the exclusion you mention does mean CH's solution meets the requirement, your mention suggests the clearer solution is the one that uses \_TAG instead i.e.

```
Replace with regular expression

Field: _TAG

pattern: ^[\s]+|[\s]+$

Replace With: <leave empty>

```

[http://i.imgur.com/uqLtPcG.png](http://i.imgur.com/uqLtPcG.png)

> [@DetlevD](#):
>
> The same result is valid for the pseudo tag-name \_FILENAME alone.

Thanks for the reminder. FTR, this is saying: 'Replace with regular expression' Field: \_FILENAME is operative on the filename.

---

<div class="post-metadata">

**Author:** ![chrishirst](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/c/4af34b/32.png) [@chrishirst](https://community.mp3tag.de/u/chrishirst)\
**Post date:** [March 24, 2014, 11:50am UTC](https://community.mp3tag.de/t/remove-leading-and-trailing-spaces-from-all-fields/15606/7 "2014-03-24T11:50:37Z")

</div>

> [@chrisjj](#):
>
> Is there a way to do this in a way that works regardless of what **fields are in the tracks**?

> [@chrisjj](#):
>
> The stated requirement is to remove leading and trailing spaces from all **fields** and by implication nothing else e.g. filename.

Yep, I took the "fields in the tracks" to be a literal requirement, as in "only affecting the metadata of the file".

---

<div class="post-metadata">

**Author:** ![chrisjj](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@chrisjj](https://community.mp3tag.de/u/chrisjj)\
**Post date:** [March 24, 2014, 11:50am UTC](https://community.mp3tag.de/t/remove-leading-and-trailing-spaces-from-all-fields/15606/8 "2014-03-24T11:50:59Z")

</div>

> [@Chris Hirst](#):
>
> Readability for one, you have to be careful with putting spaces in a regex, so using /s in a character class makes it obvious that it is intended to match a space

OK, thanks. I personally find the space form more readable - through being more compact and literal.

> [@Chris Hirst](#):
>
> also \s is an abbreviation of the POSIX [:space:] class, and will match ANY white space characters, such as ASCII codes VT, TAB, FF etc. whereas a literal space only matches a space.

Then I think the space form is the better match for the stated requirement. Plus presumably those other white space characters won't show in Mp3tag like space characters, and I'd prefer a solution that removed just the characters that look like space characters. If any of the others did end up if my fields, I'd want to see them and deal with them and their cause directly.

---

<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:** [March 24, 2014, 3:08pm UTC](https://community.mp3tag.de/t/remove-leading-and-trailing-spaces-from-all-fields/15606/9 "2014-03-24T15:08:16Z")

</div>

> [@chrisjj](#):
>
> ... 'Replace with regular expression' Field: \_FILENAME is operative on the filename.

Should better be described as:

'Replace with regular expression'  
Pseudo Field \_FILENAME is operative on the filename including fileextension.

DD.20140324.1708.CET

---

<div class="post-metadata">

**Author:** ![chrisjj](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@chrisjj](https://community.mp3tag.de/u/chrisjj)\
**Post date:** [March 24, 2014, 5:02pm UTC](https://community.mp3tag.de/t/remove-leading-and-trailing-spaces-from-all-fields/15606/10 "2014-03-24T17:02:54Z")

</div>

Ah yes, good point. One must remember that (int this special case, Action type 'Replace with regular expression') \_FILENAME does not correspond to %\_FILENAME%, but to %\_FILENAME\_EXT%.

Florian, how about resolving this by (just in this Action type, 'Replace with regular expression') adding \_FILENAME\_EXT and removing \_FILENAME on this list of allowed 'fields'\* and make the dialog change existing \_FILENAME to \_FILENAME\_EXT. Obviously for compatibility with user configs, the program would have to continue to accept \_FILENAME stored in actions.

- I previously wrote "The stated requirement is to remove leading and trailing spaces from all fields and by implication nothing else e.g. filename." but now I see that the Action dialogue box classifies the filename as a field. Misleading!

---

<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:24pm UTC](https://community.mp3tag.de/t/remove-leading-and-trailing-spaces-from-all-fields/15606/11 "2026-02-09T12:24:58Z")

</div>

Closed on February 9, 2026, 12:24pm UTC


