regex - 在 Mac 终端上使用 Regex 重命名文件

标签 regex regex-lookarounds file-rename batch-rename

我正在尝试重命名文件夹中的文件,该文件夹中的文件名称如下:

['A_LOT_OF_TEXT'].pdf&blobcol=urldata&blobtable=MungoBlobs

  • 我基本上想获取“.pdf”之前的所有内容,包括“.pdf”

安装 brew rename 后,我尝试使用它但它不起作用:

rename -n -v  '.*?(.pdf)' *

我得到错误:

Using expression: sub { use feature ':5.28'; .*?(.pdf) }
syntax error at (eval 2) line 1, near "; ."

有什么解决办法吗?

最佳答案

你当然想要

rename -n -v  's/^(.*?\.pdf).*/$1/' *

参见 regex proof .一旦您确定该表达式适合您,请删除 -n

解释

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (                        group and capture to $1:
--------------------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
--------------------------------------------------------------------------------
    \.                       '.'
--------------------------------------------------------------------------------
    pdf                      'pdf'
--------------------------------------------------------------------------------
  )                        end of $1
--------------------------------------------------------------------------------
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))

关于regex - 在 Mac 终端上使用 Regex 重命名文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67655823/

相关文章:

javascript - JSLint - 在 '\' 之前出现意外的 '.'

python - 用于匹配 HTML 特定元素的正则表达式

bash - 使用 shell 脚本重命名文件

powershell - 仅替换文件名中第一次出现的字符

regex - .htaccess 将目录重定向到子目录

regex - 在 Perl 中使用正则表达式获取字符串中的多个匹配项

javascript - 用于测试数字中加号或减号的正则表达式

linux - 如何移动带有(.JPEG、.JPG、.jpeg、.jpg)扩展名的单个文件并使用 Linux bash 将扩展名更改为 .jpg

c# - 使用正则表达式删除字符前后的空格

Amazon Redshift 中匹配序列数字的正则表达式