Finding first occurence of string


#1

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


#2
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


#3

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


#4

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


#5

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.

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


#6

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


#7

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 =