來自: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.
【這個表達式的意思是:建立三個分組,第一個是第一個單詞,第二個是第二個單詞,第三個是剩餘的東西。】