How to remove dots from file names except file extension

I have a bunch of files that look like this:

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

I want to convert via regex to:

 A File With Dots Instead Of Spaces.Extension 

It must be in one regex (because I want to use it using the Total Commander package rename tool).

Help me, regular expression guru, you are my only hope.

Edit

Several people have suggested two-stage solutions. Two steps really make this problem trivial, and I really hoped to find a one-step solution that would work in TC. I was able, BTW, to find a one-step solution that works until there are an even number of points in the file name. So I still hope for the expression of a silver bullet (or proof / explanation of why this is absolutely impossible).

+4
source share
6 answers

Here one is based on your almost decision:

 /\.([^.]*(\.[^.]+$)?)/\1/ 

Roughly speaking, "any point, minus a point, and possibly plus one more point at the end of the line." I couldn’t say whether you want the dots to be deleted or turned into spaces - if the latter, replace the substitution with "\ 1" (minus the quotes, of course).

[Edited to change + to a *, as shown below.]

+1
source

It seems that Total Commander regex library does not support inverse expressions, so you probably have to replace several points at a time until there are no points left. Replace:

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

with

 $1 $2 $3.$4 

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

There seems to be no way to do this with a single, final expression in Total Commander, sorry.

+4
source

Basically:

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

will do it in pure terms. This means that any period is replaced, followed by a string of characters (not greedy), and then a period without anything. This is a positive result .

In PHP, this is done as:

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

Other languages ​​differ, but the principle is the same.

+1
source

Or replace all points with a space, then replace [space] [Extension] with. [Expansion]

A.File.With.Dots.Instead.Of.Spaces.Extension to a file with points instead of expanding spaces to a file with points instead of spaces. Expansion

+1
source

You can do this with Lookahead. However, I don’t know what regular expression support you have.

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

Which roughly translates to Any dot /\./ , which has something and a dot after. Obviously, the last point is the only one that is not respected. I exclude the “optionality” of something between the points, because the data looks so that something will always be between them, and the “optionality” has a performance cost.

Check: http://www.regular-expressions.info/lookaround.html

0
source

Another template for finding all points except the last in the file name (windows) that I found works for me in Mass File Renamer, is:

 (?!\.\w*$)\. 

I don’t know how useful this is to other users, but this page was an early search result, and if it were here it would save me some time.

This excludes the result if it is followed by a continuous sequence of alphanumeric characters leading to the end of the input (file name), but otherwise finds all instances of the period character.

0
source

Source: https://habr.com/ru/post/1286516/


All Articles