在 Mac 终端上使用正则表达式重命名文件

时间:2021-05-23 03:15:33

标签: 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 "; ."

有什么解决办法吗?

1 个答案:

答案 0 :(得分:1)

你当然想要

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