- AFTER all upper case / Or before uppercaseLowercase

A swapping-ground for Regular Expression syntax

- AFTER all upper case / Or before uppercaseLowercase

Postby MaPo » Thu May 30, 2013 11:18 am

Hi there,

I've got a tough one to crack.

I want to add a " - " after each artist name but before the Title to seperate artist from Title.
As you can see the name of the artists are all uppercase and the title is usually first character uppercase and second lowercase.

SF175-03 - JENNIFER LOPEZ My Love Don't Cost A Thing.cdg
SF175-03 - JENNIFER LOPEZ My Love Don't Cost A Thing.mp3
SF175-01 - ANASTACIA Not That Kind .cdg
SF175-01 - ANASTACIA Not That Kind .mp3

Can somebody help me with this?

Thank You

Maurice
MaPo
 
Posts: 1
Joined: Thu May 30, 2013 11:13 am

Re: - AFTER all upper case / Or before uppercaseLowercase

Postby Stefan » Thu May 30, 2013 3:33 pm

Interesting! First I thought it is not possible... but then I saw a way.

BEFORE:
SF175-03 - JENNIFER LOPEZ My Love Don't Cost A Thing.cdg
SF175-03 - JENNIFER LOPEZ My Love Don't Cost A Thing.mp3
SF175-01 - ANASTACIA Not That Kind .cdg
SF175-01 - ANASTACIA Not That Kind .mp3


AFTER:
SF175-01 - ANASTACIA - Not That Kind .cdg
SF175-01 - ANASTACIA - Not That Kind .mp3
SF175-03 - JENNIFER LOPEZ - My Love Don't Cost A Thing.cdg
SF175-03 - JENNIFER LOPEZ - My Love Don't Cost A Thing.mp3


RULE:
I want to add a " - " after each artist name but before the Title to separate artist from Title.
As you can see the name of the artists are all upper case and the title is usually first character upper case and second lower case.



USE:
RegEx(1)
Match: (.+?[A-Z]+) ([A-Z][a-z]+.+)
Replace: \1 - \2


Explanation:
Match non-greedy everything, followed by one-or-more upper case chars followed by a blank, store that in backreference group one.
Then match one upper case char followed by one-or-more lower case chars, followed by one-or-more of any signs, store in group two.

That regex will match
(.+?[A-Z]+) ([A-Z][a-z]+.+)
SF175-03 - JENNIFER LOPEZ My Love Don't Cost A Thing.cdg
SF175-03 - JENNIFER LOPEZ My Love Don't Cost A Thing.mp3
SF175-01 - ANASTACIA Not That Kind .cdg
SF175-01 - ANASTACIA Not That Kind .mp3


Note: that may work only with non-umlauts chars. Only with standard english chars.

In the replacement we add what was matched in group one, then literal blank-hyphen-blank by ourself, followed by what was matched in group two.



- - -



A more complex but also more weird example to extend this regex to cover more of different file names:


BEFORE:
SF175-01 - ANASTACIA Not That Kind .cdg
SF175-01 - ANASTACIA Not That Kind .mp3
SF175-03 - JENNIFER LOPEZ My Love Don't Cost A Thing.cdg
SF175-03 - JENNIFER LOPEZ My Love Don't Cost A Thing.mp3
SF175-09 - U2 Stuck In a Moment With You.cdg
SF175-10 - A1 No More.cdg
SF175-11 - MARTINE McCUTCHEN On The Radio.mp3
SF999-98 - BOB DYLAN I Can't Get You Off Of My Mind.mp3
SF999-99 - BOB DYLAN A Fool Such As I.mp3


AFTER:
SF175-01 - ANASTACIA - Not That Kind .cdg
SF175-01 - ANASTACIA - Not That Kind .mp3
SF175-03 - JENNIFER LOPEZ - My Love Don't Cost A Thing.cdg
SF175-03 - JENNIFER LOPEZ - My Love Don't Cost A Thing.mp3
SF175-09 - U2 - Stuck In a Moment With You.cdg
SF175-10 - A1 - No More.cdg
SF175-11 - MARTINE - McCUTCHEN On The Radio.mp3
SF999-98 - BOB DYLAN - I Can't Get You Off Of My Mind.mp3
SF999-99 - BOB DYLAN - A Fool Such As I.mp3



USE:
RegEx(1)
Match: (.+?(?:[A-Z]\d|[A-Z]*)) ([A-Z] .+|[A-Z][a-z][A-Z]+.+|[A-Z][a-z]+.+)
Replace: \1 - \2

Explanation:

1. group
(.+?(?:[A-Z]\d|[A-Z]*))
consists of this parts
(.+?    (?:   [A-Z]\d   |   [A-Z]*)   )  

So we match non-greedy one-or-more of any sign, followed by one upper case char followed by one digit, OR,
one-or-more of upper case chars until an space,
to group one.




2. group
([A-Z] .+|[A-Z][a-z][A-Z]+.+|[A-Z][a-z]+.+)
consists of this parts
([A-Z] .+   |   [A-Z][a-z][A-Z]+   .+   |   [A-Z][a-z]+   .+)

Then we match one upper case char followed by one blank followed by one-or-more of any sign, OR,
one upper one lower one-or-more upper case chars followed by one-or-more of any sign, OR,
one upper case char followed by one-or-more lower case chars followed by one blank followed by one-or-more of any sign,
to group two.


Note: YMMV, depending on your file names...
But the not covered files may be a few only and can be modified by your self, right?


.
Stefan
 
Posts: 736
Joined: Fri Mar 11, 2005 7:46 pm
Location: Germany, EU

Re: - AFTER all upper case / Or before uppercaseLowercase

Postby truth » Sat Jun 29, 2013 12:32 am

Another way is to match everything until the Last2Uppers:
(.+[A-Z]{2,})(.+)
\1 -\2

To capture GroupNames ending like U2, you could match either: Last2Uppers OR LastUpperNumbers:
(.+ -) (.*[A-Z]{2,}|[A-Z]{1,}[0-9]{1,})(.+)
\1 \2 -\3
truth
 
Posts: 221
Joined: Tue Jun 25, 2013 3:39 am
Location: Earth, OrionArm, MilkyWay

Re: - AFTER all upper case / Or before uppercaseLowercase

Postby Venelin » Mon Jan 26, 2015 7:43 pm

Hi,
Please Help

How to rename for example MADONNA - VOGUE.mp3 to MADONNA -Vogue.mp3
or ANTON ISHUTIN - CRAZY FOR YOU.mp3 to ANTON ISHUTIN - Crazy For You.mp3

Thanks :)
Venelin
 
Posts: 1
Joined: Mon Jan 26, 2015 7:38 pm


Return to Regular Expressions


cron