Replace position of strings in a file name

A swapping-ground for Regular Expression syntax

Replace position of strings in a file name

Postby alba77 » Sat May 02, 2020 12:41 pm

Hello,
I justo downloaded this powerful and complicated utility.
I tried some of your examples but there were no way to make it work.
So I have a call recrdings that I have to sort by date but the date is at the end of the file name

Thi files are in this way before the extension whitch I don't have to touch:

Registrazione chiamata Acqua & Sapone_200428_124545
Registrazione chiamata Coop Voce Assistenza_200427_223721
Registrazione chiamata Tigre_200429_133120
Registrazione chiamata ? My husband ?_200427_170057
Registrazione chiamata Private number_200502_132916

The green part meand call recording and I want it to be deleated
The blue part in the name of the contact
The black the underscore "_"
The red part is the Date and the hour.

So I'd need to Move the red at the beginning and the name/number without the "_" of the contact and adding in the middle " - " like this:
200502_132916 - Private number
200427_170057 - ? My husband ?

May you kindy suggest me a code to use?
alba77
 
Posts: 3
Joined: Sat May 02, 2020 12:22 pm

Re: Replace position of strings in a file name

Postby alba77 » Sat May 02, 2020 12:45 pm

Srry but I don't find a function to correct a mestake:
I'd like to correct this in my prevouse message.
The green part meand = The green part meanS
alba77
 
Posts: 3
Joined: Sat May 02, 2020 12:22 pm

Re: Replace position of strings in a file name

Postby therube » Sat May 02, 2020 2:35 pm

1:RegEx
Code: Select all
Match:  (.*?)_(.*)
Replace:  \2 - \1


3:Replace
Code: Select all
Repace:  registrazione chiamata <--- there is a trailing space there
With:  <enter nothing here>


Remove, registrazione chiamata
(then) match anything up to the first _
(then) match everything else

Then just rearrange the two matches & stick a - in between.


(I didn't really expect it to work as I have done, but it does.
I thought 1:RegEx would take precedence, but... seems it didn't.)
therube
 
Posts: 1314
Joined: Mon Jan 18, 2016 6:23 pm

Re: Replace position of strings in a file name

Postby alba77 » Sat May 02, 2020 4:39 pm

Thank you very much therube,
It worked perfectly.

Now Id like to ask you someting about the code:
The replace function (3) was clear to me.

Separating and Matching the parts was the most dificult.

Also the replace in the RegEx Was clear to me.
So the match is: (.*?)_(.*)
I know that * meand everything and the . is the separator before the extension.
In our situation what the . and the ? are and why the 1st part has the ? and the 2on one doen't have it?

What abou if I would like to thing in other way and if I would like to ask it to move the last 13characters to the beginning and after separate with " - \1" ?
alba77
 
Posts: 3
Joined: Sat May 02, 2020 12:22 pm

Re: Replace position of strings in a file name

Postby therube » Mon May 04, 2020 12:05 am

the . is the separator before the extension

Not here, not with regex.
In this case (.) means any character.
And the (*) means 0 or more of that character.

So you have any character & any number of any characters.

(?) constrains what is found, non-greedy.
(default is greedy, picking up everything)

.* matches anything & everything.
.*? matches anything & everything - with constraints (non-greedy).

(.*)_
find any character & any number of them - up to the (last) instance of _

(.*?)_
find any character & any number of them - constrained, up to the (first) instance of _

So with: abc123_456_789

(.*)_
finds: abc123_456 <- everything up to the (last) _

(.*?)_
finds: abc123 <- everything, constrained, up to the (first) _
therube
 
Posts: 1314
Joined: Mon Jan 18, 2016 6:23 pm


Return to Regular Expressions


cron