22
10 月
2023

批量重命名工具 — Bulk Rename Utility教程

【Bulk Rename Utility 是有效的、简洁的文件名批量替换工具,但如果想发挥它的最大功效,还是得学会正则表达式,Bulk Rename Utility 的正则表达式是 Perl Regular Expression。下面是一个实例。】

Assume you have a file called Program Files, and you wish to swap the names around (e.g. Files Program). A Regular Expression which performs this task is :

【假设我们有一个文件叫做 Program Files  ,我们想交换一下,改为 Files Program,那么Perl正则表达式应为:】

^([A-Z][a-z]*) ([A-Z][a-z]*)

Let us break this down into components:

【让我们拆解一下:】

This means start at the beginning of the string

^意思是字符串开始处】

([A-Z][a-z]*) This is a single “group”, which we will use later. What this says is that we want any capital letter, followed by any number of lower-case letters. The single capital letter is denoted by the [A-Z], i.e. allow a letter in the range A to Z. The lower-case letters are denoted by [a-z] in the same way, followed by an asterisk. This means we can allow any number of letters.

([A-Z][a-z]*) 是一个独立“组”。其含义是:我们想找到所有大写字母,这些大写字母后面是小写字母。一个大写字母用 [A-Z] 代表,也就是允许A到Z中的任一大写字母。一个小写字母用 [a-z] 代表,也就是允许a到z的任一小写字母。最后的 星标,表示我们可以不限制满足以上条件的字母数量,可以是一个,也可以是多个。】

We then allow a single space. If I had wanted multiple spaces I would probably have typed “space asterisk”, or possible ( *) to group.

【然后,我们在第一个独立组后面加一个空格。如果希望不止一个空格,那么应该用“空格+星标”,即 ( *) 表明,这里用了括号表示又一个独立组。】

We then have exactly the same again, i.e. we are denoting two words.

【再然后,我们重复了第一组的表达式,也就是说我们的目标对象是两个情况一样的单词:Program Files 。】

Notice we had two sets of brackets. Everything within each set of brackets is treated as a “grouping”, and we refer to these groupings as \1\2\3 etc.

【注意我们有两套括号,每对括号中的内容被视为一个“分组”,这样就可以用  \1\2\3  来引用他们。】

So, lets say we wanted to swap around the two words in the filename. We would put:

【这样,我们的交换表达式中,匹配部分就可以这样写:】

^([A-Z][a-z]*) ([A-Z][a-z]*)

For the match string, and

【替换部分这样写:】

\2 \1

The above example is very precise. If we wanted to swap the first two words of a name, but keep the remaining text the same, we could put

【上面的例子很简单,如果我们有一个文件名为:Program Files Directory ,我们只想交换前两个单词,剩下的部分保持不变,即成为:Files Program Directory,那么我们应该这样写:】

^([A-Z][a-z]*) ([A-Z][a-z]*)(.*)

\2\1\3

This says to create three groups: the first group is the first word, the second group is the second word, and the third group is everything that’s left.

【这个表达式的意思是:创建三个分组,第一个是第一个单词,第二个是第二个单词,第三个是剩余的东西。】

回到顶部

Perl 正则表达式 在 Bulk Rename Utility 实例

You may also like...

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注