# Split a string on the \*first\* space?

**URL:** https://community.mp3tag.de/t/split-a-string-on-the-first-space/8520
**Category:** Support
**Created:** [June 28, 2009, 5:52am UTC](https://community.mp3tag.de/t/split-a-string-on-the-first-space/8520 "2009-06-28T05:52:37Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![JJ\_Johnson](https://community.mp3tag.de/user_avatar/community.mp3tag.de/jj_johnson/32/67_2.png) [@JJ\_Johnson](https://community.mp3tag.de/u/JJ_Johnson)
#### Post date: [June 28, 2009, 5:52am UTC](https://community.mp3tag.de/t/split-a-string-on-the-first-space/8520/1 "2009-06-28T05:52:37Z")

</div>

I use the following regex (probably in a FAQ somewhere) for splitting names into a first and last name, to create 'Firstname, Lastname' ARTISTSORT fields:

^(.+)\s(.+)$ -\> $2, $1

This works well for most names, even those where the artist has a middle name, since it splits on the last space in the string:

John Lee Hooker --\> Hooker, John Lee

The trouble is that it doesn't handle cases where an artist has a two words in their last name, e.g.

Townes Van Zandt --\> Zandt, Townes Van

This should instead be

Van Zandt, Townes

So I need a regex that will split strings on the first space instead of the last.

---

<div class="post-metadata">

### Author: ![dano](https://community.mp3tag.de/user_avatar/community.mp3tag.de/dano/32/6_2.png) [@dano](https://community.mp3tag.de/u/dano)
#### Post date: [June 28, 2009, 6:33am UTC](https://community.mp3tag.de/t/split-a-string-on-the-first-space/8520/2 "2009-06-28T06:33:29Z")

</div>

Just add a question mark:  
^(.+?)\s(.+)$ -\> $2, $1

---

<div class="post-metadata">

### Author: ![JJ\_Johnson](https://community.mp3tag.de/user_avatar/community.mp3tag.de/jj_johnson/32/67_2.png) [@JJ\_Johnson](https://community.mp3tag.de/u/JJ_Johnson)
#### Post date: [June 28, 2009, 6:43am UTC](https://community.mp3tag.de/t/split-a-string-on-the-first-space/8520/3 "2009-06-28T06:43:32Z")

</div>

Thanks. I also found that this works:

^([^\s]+)\s(.+)$ -\> $2, $1

but yours looks simpler. I take it that the question mark must make the + non-greedy.

---

<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:21pm UTC](https://community.mp3tag.de/t/split-a-string-on-the-first-space/8520/4 "2026-02-09T12:21:36Z")

</div>


