# Finding first occurence of string

**URL:** https://community.mp3tag.de/t/finding-first-occurence-of-string/3876
**Category:** Support
**Created:** [October 9, 2006, 3:37am UTC](https://community.mp3tag.de/t/finding-first-occurence-of-string/3876 "2006-10-09T03:37:38Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![juiceman](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/j/ee7513/32.png) [@juiceman](https://community.mp3tag.de/u/juiceman)
#### Post date: [October 9, 2006, 3:37am UTC](https://community.mp3tag.de/t/finding-first-occurence-of-string/3876/1 "2006-10-09T03:37:38Z")

</div>

Hi, this is a newbie question.

I have string which may come in the following formats:

string1 (string2 - string3)  
string1 (string2/string2 - string3)  
string1 (string2/string3/string4)  
string1 (string2)

I always to grab string2 with replace with regular expression.

I am doing the following:

^(._)((._)/-)$

to get $2

I guess I don't understand the priority of the bracket statement. How do I make it choose the first occurence (leftmost) of those choices. In the third example, there are multiple forward slashes and I want to choose the first.

Thanks

---

<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 9, 2006, 5:55am UTC](https://community.mp3tag.de/t/finding-first-occurence-of-string/3876/2 "2006-10-09T05:55:15Z")

</div>

> [@juiceman](#):
>
> ... I guess I don't understand the priority of the bracket statement. ...

```
s1: string1 (string2 - string3)
s2: string1 (string2/string3 - string4)
s3: string1 (string2/string3/string4)
s4: string1 (string2)

r1: ^(.+) \((.+) - (.+)\)$ ==> $1 $2 $3
r2: ^(.+) \((.+)\/(.+) - (.+)\)$ ==> $1 $2 $3 $4
r3: ^(.+) \((.+)\/(.+)\/(.+)\)$ ==> $1 $2 $3 $4
r4: ^(.+) \((.+)\)$ ==> $1 $2

```

I hope it is self explaining.

DD.2006109.0955

---

<div class="post-metadata">

### Author: ![juiceman](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/j/ee7513/32.png) [@juiceman](https://community.mp3tag.de/u/juiceman)
#### Post date: [October 9, 2006, 11:41am UTC](https://community.mp3tag.de/t/finding-first-occurence-of-string/3876/3 "2006-10-09T11:41:00Z")

</div>

Sorry for not explaining clearly. I have no idea which of the 4 examples may occur. Any of them may happen and I want to handle them with one replace command. Or else I will have to manually sort them and apply a different action for each one. I was hoping to avoid this.

QUOTE (DetlevD @ Oct 9 2006, 01:55) \<{POST\_SNAPBACK}\>

 s1: string1 (string2 - string3)

```
s2: string1 (string2/string3 - string4)
s3: string1 (string2/string3/string4)
s4: string1 (string2)

r1: ^(.+) \((.+) - (.+)\)$ ==> $1 $2 $3
r2: ^(.+) \((.+)\/(.+) - (.+)\)$ ==> $1 $2 $3 $4
r3: ^(.+) \((.+)\/(.+)\/(.+)\)$ ==> $1 $2 $3 $4
r4: ^(.+) \((.+)\)$ ==> $1 $2

```

I hope it is self explaining.

DD.2006109.0955

---

<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 9, 2006, 12:56pm UTC](https://community.mp3tag.de/t/finding-first-occurence-of-string/3876/4 "2006-10-09T12:56:26Z")

</div>

> [@juiceman](#):
>
> Sorry for not explaining clearly. I have no idea which of the 4 examples may occur. Any of them may happen and I want to handle them with one replace command. ...

Give this a try (not tested):  
RegExp: **^(.+) ((.+)( - (.+)|/(.+) - (.+)|/(.+)/(.+)|))$**

In Mp3tag it should be: **^(.+) ((.+)( - (.+)|/(.+) - (.+)|/(.+)/(.+)|()))$**

When running this RegExp in Mp3tag reality it produces not the result I would expect.  
So please have patience, I will think over, maybe some other RegExp crack can help in the meantime.

DD.20061009.1656

---

<div class="post-metadata">

### Author: ![juiceman](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/j/ee7513/32.png) [@juiceman](https://community.mp3tag.de/u/juiceman)
#### Post date: [October 9, 2006, 2:22pm UTC](https://community.mp3tag.de/t/finding-first-occurence-of-string/3876/5 "2006-10-09T14:22:43Z")

</div>

DD

Thanks for your help. I think I have it. I tested this on EditPad Pro at work. Now I need to go home and see if it works in MP3TAG.

^(._)((._?)/( - )\\$

---

<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 10, 2006, 2:58am UTC](https://community.mp3tag.de/t/finding-first-occurence-of-string/3876/6 "2006-10-10T02:58:06Z")

</div>

> [@juiceman](#):
>
> ... Thanks for your help. I think I have it. ...

So far so good.

But while I'm struggling with the Mp3tag RegExp machine there were coming up new questions.

Literal string: **A=B=C=**  
RegExp: **(.+=)(.+=)**  
Result $1: **A=B=**  
Result $2: **C=**

I have expected that RegExp's would be evaluated from left to right, and the result should be:  
$1: **A=**  
$2: **B=C=**

Is there anybody out there who could explain this behaviour?

DD.20061010.0658

---

<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: [October 10, 2006, 5:45am UTC](https://community.mp3tag.de/t/finding-first-occurence-of-string/3876/7 "2006-10-10T05:45:21Z")

</div>

Quantifiers are greedy and try to match as much as possible.  
Your (.+=) does not stop at the first = it goes till the end of the string and back to the second because .+ matches every sign.  
To make the quantifer lazy you can add a question mark behind it (.+?=) and it stops at the first =

---

<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:19pm UTC](https://community.mp3tag.de/t/finding-first-occurence-of-string/3876/8 "2026-02-09T12:19:55Z")

</div>


