# RegExp to extract text before first comma

**URL:** https://community.mp3tag.de/t/regexp-to-extract-text-before-first-comma/57083
**Category:** General Discussion
**Tags:** howto
**Created:** [April 19, 2022, 9:45pm UTC](https://community.mp3tag.de/t/regexp-to-extract-text-before-first-comma/57083 "2022-04-19T21:45:08Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![EricB](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/e/f19dbf/32.png) [@EricB](https://community.mp3tag.de/u/EricB)
#### Post date: [April 19, 2022, 9:45pm UTC](https://community.mp3tag.de/t/regexp-to-extract-text-before-first-comma/57083/1 "2022-04-19T21:45:08Z")

</div>

I use MP3TAG to set the metadata and file name for my audiobooks. For the file name, I like as much info about the book without making it too long. It is in the format

Author -- Title (Narrator) (Chapter/track).mp3 \<- Narrator is stored in %composer%

I had an issue in that some books have multiple narrators (e.g., Frank Mueller, David Cummings, Andrea Martin) and I didn't want the file name to be too long, so I just wanted the first narrator in the list from the metadata. So, I use that TAG -\> FILENAME tool and include a REGEXP expression to pull only the first narrator, as follows:

`%artist% -- %title% ($regexp(%composer%,^(.+?)\x{002c}(.*),$1)) (Ch%track%)`

The REGEXP portion [$regexp(x,e,r,c)] is this

`$regexp(%composer%,^(.+?)\x{002c}(.*),$1)`

where

| | |
| --- | --- |
| `$regexp(` | string function to start regular expression |
| `%composer%` | x entry, initial string where the narrators are stored |
| `^(.+?)\x{002c}(.*)` | e entry, the regular expression |

| | |
| --- | --- |
| `^` | start at beginning of string |
| `(.+?)` | extract any character non-greedy → this goes into `$1` |
| `\{002c}` | up to first comma (comma must be escaped or Unicode) -\> not stored |
| `(.*)` | extract everything else → this goes into `$2` |

| | |
| --- | --- |
| `$1` | r entry, The field to be displayed |

Hope this helps others.

---

<div class="post-metadata">

### Author: ![ohrenkino](https://community.mp3tag.de/user_avatar/community.mp3tag.de/ohrenkino/32/4843_2.png) [@ohrenkino](https://community.mp3tag.de/u/ohrenkino)
#### Post date: [April 20, 2022, 6:23am UTC](https://community.mp3tag.de/t/regexp-to-extract-text-before-first-comma/57083/2 "2022-04-20T06:23:20Z")

</div>

> [@EricB](#):
>
> |`\{002c}`|up to first comma (comma must be escaped or Unicode)

This is not quite true if you use  
`$regexp(%composer%,'^(.+?),.*',$1)`  
instead  
The potential section following the comma does not necessarily need the parenthesis if you do not want to use it anyway, so $2 is not mandatory.  
... but there are so many ways to solve the problem.

---

<div class="post-metadata">

### Author: ![mp3freezetag](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/m/c77e96/32.png) [@mp3freezetag](https://community.mp3tag.de/u/mp3freezetag)
#### Post date: [October 27, 2023, 5:24am UTC](https://community.mp3tag.de/t/regexp-to-extract-text-before-first-comma/57083/3 "2023-10-27T05:24:50Z")

</div>

incredibly helpful, thank you. it worked for me with songs that have multiple artists, which would mess up the filing convention I use (artist, album, songs)
