I have a bunch of file names like this a1_b2_c3_d4.png
but I want them to renamed like this a1_b2_d4_c3.png
. I am having NameChanger application to implement regex on file names. I could not figure out regex to swap c3
and d4
. If anyone can help me with the regex, that will be great.
Asked
Active
Viewed 103 times
0

Wiktor Stribiżew
- 607,720
- 39
- 448
- 563

Stu M
- 19
- 5
-
Maybe this is a question for https://superuser.com/? – SamWhan Apr 25 '17 at 14:25
-
`(\w+)_(\w+)(\.\w+)$` -> `$2_$1$3` – Wiktor Stribiżew Apr 25 '17 at 14:26
-
Show the code you've tried; it's unclear what the issue is... – l'L'l Apr 25 '17 at 14:27
-
Check https://regex101.com/r/u1IPQK/1 – SamWhan Apr 25 '17 at 14:27
-
Here's an attempt: https://regex101.com/r/ZJlap4/2/. Not sure if NameChanger supports lookaheads, though. – Joseph Marikle Apr 25 '17 at 14:29
-
`(\w+)_(\w+)(\.\w+)$ -> $2_$1$3` is close. But still not able to do what I want. This expression is placing the last value in the front. I want to swap and 3rd and 4th value in the name. – Stu M Apr 25 '17 at 14:34
-
Have you tried my suggestion? `(^.*_)(\w+)_(\w+)(\..*)$` -> `$1$3_$2$4` illustrated [here at regex101](https://regex101.com/r/u1IPQK/3/)? – SamWhan Apr 25 '17 at 14:43
1 Answers
0
There are three permutations to what you ask:
- If you only care about the last 2 before the dot
find ([^._]+)_([^._]+)\.
replace $2_$1.
- If it has to be exactly the 3 rd or 4 th before the dot
find ^([^._]+_[^._]+_)([^._]+)_([^._]+)\.
replace $1$3_$2.
- If it has to be exactly the 3 rd or 4 th without dot
find ^([^._]+_[^._]+_)([^._]+)_([^._]+)
replace $1$3_$2