Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to eliminate dots from filenames, except for the file extension

I have a bunch of files that look like this:

A.File.With.Dots.Instead.Of.Spaces.Extension

Which I want to transform via a regex into:

A File With Dots Instead Of Spaces.Extension

It has to be in one regex (because I want to use it with Total Commander's batch rename tool).

Help me, regex gurus, you're my only hope.

Edit

Several people suggested two-step solutions. Two steps really make this problem trivial, and I was really hoping to find a one-step solution that would work in TC. I did, BTW, manage to find a one-step solution that works as long as there's an even number of dots in the file name. So I'm still hoping for a silver bullet expression (or a proof/explanation of why one is strictly impossible).

like image 342
Assaf Lavie Avatar asked Jan 23 '23 12:01

Assaf Lavie


2 Answers

It appears Total Commander's regex library does not support lookaround expressions, so you're probably going to have to replace a number of dots at a time, until there are no dots left. Replace:

([^.]*)\.([^.]*)\.([^.]*)\.([^.]*)$

with

$1 $2 $3.$4

(Repeat the sequence and the number of backreferences for more efficiency. You can go up to $9, which may or may not be enough.)

It doesn't appear there is any way to do it with a single, definitive expression in Total Commander, sorry.

like image 160
molf Avatar answered Jan 26 '23 01:01

molf


Basically:

/\.(?=.*?\.)//

will do it in pure regex terms. This means, replace any period that is followed by a string of characters (non-greedy) and then a period with nothing. This is a positive lookahead.

In PHP this is done as:

$output = preg_replace('/\.(?=.*?\.)/', '', $input);

Other languages vary but the principle is the same.

like image 26
cletus Avatar answered Jan 26 '23 01:01

cletus