by Glenn » Sun Dec 11, 2011 6:32 pm
There are many examples similar to this in the forum.
In any case, the pattern you have shown in your files is such that you want the part of the filename that is after the last underscore swapped to the front. Some may have 2 underscores, some one.
We can use a regex such as:
(.+)_(.+)
To grab the last (right most) underscore we can take advantage of the fact the normal regex (.+) is greedy and first matches the entire string. Then it works backwards (backtracks) from the end until it can match the _ and takes the first _ it finds which will be the right hand one. It captures the last piece after that in the second (.+) which is \2, the second captured group.
Now the first (.+) contains everything up to the last underscore, and it is the first captured group \1.
(Putting parentheses around sections of the match cause it to be captured and given names \1, \2, etc counting the groups of parentheses from left to right. These captured groups can then be used in the replace. This is one of the advantages of Regex)
Replace with:
\2_\1
There are other ways of doing this, and would require modification if the data assumptions are changed.
Cheers,
Glenn
Last edited by
Glenn on Mon Dec 19, 2011 10:35 pm, edited 1 time in total.