在ZMV REGEX范围过滤器中包括周期字符(“。”)

发布于 2025-01-20 14:22:24 字数 907 浏览 3 评论 0原文

我正在尝试编写一个简单的 cronjob,通过文件夹和目录进行递归。嵌套在 /OneDrive 中的文件,并替换 AZaz的文件或文件夹名称字符0-9,或 . 加上 _。 (这让我觉得这是解决同步错误的最简单方法。)

构建 这篇 StackExchange 帖子 我已经成功地使用了其中的大部分方式:

zmv '**/*' '$f:h${${f:t}//[^A-Za-z0-9]/_}'

但是,此正则表达式排除 的,这导致我所有文件夹中的所有文件扩展名都更改为 _(例如 file.txt 变为 file_txt)。

我距离高级正则表达式用户还很远,并且我使用的该命令的所有各种排列都引发了错误,包括:


zmv '**/*' '$f:h${${f:t}//[^\.A-Za-z0-9]/_}'
zmv '**/*' '$f:h${${f:t}//[^.A-Za-z0-9]/_}'
zmv '**/*' '$f:h${${f:t}//[^A-Za-z0-9\.]/_}'

我确信正确的正则表达式是显而易见的;恐怕这对我来说并不明显。

如果有人可以在这里提供一些指导,并简要解释为什么我之前的尝试不起作用,我将不胜感激,因为它对我对正则表达式的理解做出了小小的贡献。

I'm trying to write a simple cronjob that recurses through the folders & files nested in /OneDrive and replaces file or folder name characters that are not A-Z, a-z, 0-9, or . with an _. (This strikes me as the easiest way to resolve syncing errors.)

Building off this StackExchange post I've managed to get most of the way there using:

zmv '**/*' '$f:h${${f:t}//[^A-Za-z0-9]/_}'

This regex expression, however, does not exclude .'s, which results in all of the file extensions in all of my folders being changed to _s (e.g. file.txt becomes file_txt).

I'm far from an advanced regex user, and all of the various permutations of this command I've used have thrown errors, including:


zmv '**/*' '$f:h${${f:t}//[^\.A-Za-z0-9]/_}'
zmv '**/*' '$f:h${${f:t}//[^.A-Za-z0-9]/_}'
zmv '**/*' '$f:h${${f:t}//[^A-Za-z0-9\.]/_}'

I'm sure the correct regex expression is obvious; I'm afraid it's just not obvious to me.

If anyone could provide some guidance here, along with a brief explanation of why my previous attempts didn't work, I'd be grateful for the small contribution to my understanding of regex.

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

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

发布评论

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

评论(1

终陌 2025-01-27 14:22:24

:h 修饰符去掉了斜线;这已将其添加回来:

zmv '**/*' '$f:h/${${f:t}//[^\.A-Za-z0-9]/_}'

我不确定为什么您引用的原始答案中缺少这一点。

一些注释/警告:

  • zmv-n 选项对于追踪此类问题非常有帮助。
  • 一次调用 zmv 可能无法同时重命名文件名和目录名;可能需要多次通过。
  • zmv 使用文件通配模式。通配模式与正则表达式类似,但存在许多显着差异,因此许多正则表达式文档此处不适用。

The :h modifier stripped out a slash; this has it added back in:

zmv '**/*' '$f:h/${${f:t}//[^\.A-Za-z0-9]/_}'

I'm not sure why that was missing from the original answer you referenced.

Some notes / caveats:

  • the -n option for zmv can be very helpful in tracking down issues like this.
  • a single call to zmv may not be able to rename both file names and directory names; multiple passes could be needed.
  • zmv uses file globbing patterns. Globbing patterns are similar to regular expressions, but there are a number of significant differences, so a lot of documentation for regexes will not apply here.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文