Switch strings in filenames with regex?

Discussion, questions and support.
Post Reply
Message
Author
eric_verboven
Posts: 23
Joined: 27.04.2015, 15:08

Switch strings in filenames with regex?

#1 Post by eric_verboven » 24.06.2016, 20:34

Hi all,

I have a bunch of files, named like this:

My life - Jim Johnson.txt
Some long title with many words - Jack Simon Gudmundson.doc
Yes - James.xls

My question: what regex do I need to put (in FC 2014 build 650) the part behind the " - " in front, and the stuff which originally was before the " - " behind it? So, the result should be:

Jim Johnson - My life.txt
Jack Simon Gudmundson - Some long title with many words.doc
James - Yes.xls

Anyone who can shed some light on this? I'm all but mute in regex, I'm afraid, so bear with me.
Thanks!

Kotty
Posts: 3
Joined: 26.06.2016, 22:21

Re: Switch strings in filenames with regex?

#2 Post by Kotty » 26.06.2016, 22:27

Hi there,

I think following lines should do the trick, however I'm no regex-pert, so keep that in mind and test it out properly before proceeding.

Search for:

Code: Select all

([\w+\s|\w+]{1,})\s\-\s([\w+\s|\w+]{1,})
Replace with:

Code: Select all

$2\ \-\ $1
Hope it helps.

Regards,
Kotty

eric_verboven
Posts: 23
Joined: 27.04.2015, 15:08

Re: Switch strings in filenames with regex?

#3 Post by eric_verboven » 28.06.2016, 01:49

Kotty wrote:Hi there,

I think following lines should do the trick, however I'm no regex-pert, so keep that in mind and test it out properly before proceeding.

Search for:

Code: Select all

([\w+\s|\w+]{1,})\s\-\s([\w+\s|\w+]{1,})
Replace with:

Code: Select all

$2\ \-\ $1
Hope it helps.

Regards,
Kotty
Hi Kothy,

Thanks for your code, which appears to do the trick in the cases I submitted. So, that's magnificent!!

But (alas, but...) there are also filenames with other characters, either in the first or in the second part, or even in both. Let's for instance take a comma.

Then the results get a bit weird - though undoubtedly not to skilled regex specialists.

Starting from this:

No comma - No problem.txt
A comma, in the first part - And a comma, in the second.txt
There is a comma, in the first part - No comma in the second.txt
There is no comma in the first part - A comma, in the second part.txt

I get this (with your code) :

No problem - No comma.txt
A comma,And a comma - in the first part, in the second.txt
There is a comma,No comma in the second - in the first part.txt
A comma - There is no comma in the first part, in the second part.txt

So, the comma (or other "special" characters) causes a hiccup. The "looked for" part appears to stop whenever there is a comma. The trick would be to make it Only stop at " - ".

But I do think your code is very close to the solution... Sadly, I'm in absolutely no position to get any closer to it myself. If you call yourself "no regex-pert", then I'm a complete and utter regex-neandertal :)

I hope you find the time to take a second look? Thanks!!

Kotty
Posts: 3
Joined: 26.06.2016, 22:21

Re: Switch strings in filenames with regex?

#4 Post by Kotty » 28.06.2016, 15:20

Hi,
I think following regex should do the trick.

Search for:

Code: Select all

([^.-]+)\s\-\s([^.-]+)
'Replace with' regex stays the same.

Regards,
Kotty

eric_verboven
Posts: 23
Joined: 27.04.2015, 15:08

Re: Switch strings in filenames with regex?

#5 Post by eric_verboven » 28.06.2016, 18:12

Kotty wrote:Hi,
I think following regex should do the trick.

Search for:

Code: Select all

([^.-]+)\s\-\s([^.-]+)
'Replace with' regex stays the same.

Regards,
Kotty
Hi Kotty,

Respectfully thanks!!! That indeed did the trick!

These filenames:
1 - 2.txt
1, a - 2.txt
1 - 2, b.txt
1, a - 2, b.txt

were neatly turned (thanks to your code) into:
2 - 1.txt
2 - 1, a.txt
2, b - 1.txt
2, b - 1, a.txt

Exactly what I was looking for! Thanks ever so much!!

Kotty
Posts: 3
Joined: 26.06.2016, 22:21

Re: Switch strings in filenames with regex?

#6 Post by Kotty » 28.06.2016, 22:56

Glad to help. :)

Kotty

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 41 guests