如何消除文件名中的点(文件扩展名除外)

发布于 2024-07-25 05:58:22 字数 467 浏览 5 评论 0原文

我有一堆看起来像这样的文件:

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

我想通过正则表达式将其转换为:

A File With Dots Instead Of Spaces.Extension

它必须位于一个正则表达式中(因为我想将它与 Total Commander 的批量重命名工具一起使用)。

帮助我,正则表达式专家,你是我唯一的希望。

编辑

有几个人建议了两步解决方案。 两个步骤确实让这个问题变得微不足道,我真的希望找到一个适用于 TC 的一步解决方案。 顺便说一句,我确实设法找到了一种一步解决方案,只要文件名中有偶数个点,该解决方案就可以工作。 所以我仍然希望有一个灵丹妙药的表达(或者证明/解释为什么一个绝对不可能)。

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

内心荒芜 2024-08-01 05:58:23

您可以使用 Lookahead 来做到这一点。 但是我不知道你有哪种正则表达式支持。

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

大致翻译为任何点/\./,后面有一些东西和一个点。 显然最后一点是唯一不符合要求的点。 我省略了点之间某些东西的“可选性”,因为数据看起来总是在点之间,并且“可选性”具有性能成本。

查看:
http://www.regular-expressions.info/lookaround.html

You can do that with Lookahead. However I don't know which kind of regex support you have.

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

Which roughly translates to Any dot /\./ that has something and a dot afterwards. Obviously the last dot is the only one not complying. I leave out the "optionality" of something between dots, because the data looks like something will always be in between and the "optionality" has a performance cost.

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

遗失的美好 2024-08-01 05:58:22

Total Commander 的正则表达式库似乎不支持环视表达式,因此您可能必须一次替换多个点,直到没有剩下的点为止。 Replace:

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

with

$1 $2 $3.$4

(重复序列和反向引用的数量以提高效率。您最多可以花 9 美元,这可能足够也可能不够。)

似乎没有任何方法可以使用单个确定的表达式来完成此操作在《全面指挥官》中,抱歉。

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.

请叫√我孤独 2024-08-01 05:58:22

基本上:

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

将以纯正则表达式的方式进行。 这意味着,将后面跟着一串字符(非贪婪)的任何句点替换为空,然后替换一个句点。 这是一个正向预测

在 PHP 中,这是这样做的:

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

其他语言有所不同,但原理是相同的。

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.

茶花眉 2024-08-01 05:58:22

这是基于您的几乎解决方案的一个:

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

大致是“任何点内容,减去点,并且可能在行末尾加上另一个点内容”。 我不太清楚你是否想要删除点或变成空格 - 如果是后者,请将替换更改为“ \1”(当然,减去引号)。

[编辑将+更改为*,如下海伦的。]

Here's one based on your almost-solution:

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

This is, roughly, "any dot stuff, minus the dot, and maybe plus another dot stuff at the end of the line." I couldn't quite tell if you wanted the dots removed or turned to spaces - if the latter, change the substitution to " \1" (minus the quotes, of course).

[Edited to change the + to a *, as Helen's below.]

甜嗑 2024-08-01 05:58:22

或者用空格替换所有点,然后用 .[Extension]

A.File.With.Dots.Instead.Of.Spaces.Extension 替换 [space][Extension]

带有点而不是空格扩展名的文件

带有点而不是空格的文件.扩展名

Or substitute all dots with space, then substitute [space][Extension] with .[Extension]

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

时光与爱终年不遇 2024-08-01 05:58:22

我发现在 Mass File Renamer 中找到对我有用的(Windows)文件名中除最后一个点之外的所有点的另一种模式是:

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

我不知道这对其他用户有多大用处,但此页面是早期搜索结果如果那是在这里的话,那会节省我一些时间。

如果结果后面跟着一个不间断的字母数字字符序列,导致输入(文件名)结束,则它会排除结果,但否则会查找点字符的所有实例。

Another pattern to find all dots but the last in a (windows) filename that I've found works for me in Mass File Renamer is:

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

I don't know how useful that is to other users, but this page was an early search result and if that had been on here it would have saved me some time.

It excludes the result if it's followed by an uninterrupted sequence of alphanumeric characters leading to the end of the input (filename) but otherwise finds all instances of the dot character.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文