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

来自:Bulk Rename Utility 文档正则表达式

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

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正则表达式应为:this

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

Let us break this down into components:component

让咱们拆解一下:orm

^ This means start at the beginning of the string对象

^意思是字符串开始处three

([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.ip

([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.

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

相关文章
相关标签/搜索