Rearranging filename

A swapping-ground for Regular Expression syntax

Rearranging filename

Postby Rexomnium » Tue Jun 10, 2014 1:52 pm

I am sorry that I need to ask this. For several hours I am trying to make this thing work, but since my programming and logical thinking skills are limited, I need to ask the experts on this. The problem is quite simple:

I have a huge amount of files which are named like this:

xxa - a variable name which can be any length - xb.txt

    The first 'xxa - ' are numbers with a leading zero if they are below 10. There are 2 spaces with a minus-sign in between. They don't surpass 99. They don't add up and there is no logical order here. For instance, there are several files which begin with '01'.
    The variable name can be any length. Sometimes there are &'s in them. There are only spaces, letters and &'s in them.
    The last - xb is optional. There are a not many (around 100) files which are named like this. The x represents a number without a leading zero. None of them surpasses 9.

I want the files renamed like this:

a variable name which can be any length - xxa - xxb

xxb need to get a leading zero. If there isn't a xxb in the original file name, I want the new file name to have - 01.

Is this possible in one regular expression and how? Thank you very much for the answer if you have any. You are all wonderful :)
Rexomnium
 
Posts: 1
Joined: Tue Jun 10, 2014 1:39 pm

Re: Rearranging filename

Postby truth » Thu Jun 12, 2014 11:57 am

No, its not possible for regex alone.
Once 01 goes into replacement, it must always be part of NewName.
The only exception would be non-matched names, but you need both conditions matched.

Here's an ugly work-around to match both conditions:

1Regex match/replace:
^(\d\d) - ([^-]*?)( - (\d))*$
\2 - \1 - 0\4

3Replace/With:
\4
1

The only limit I placed on VarNames was: not having -'s
Post back if thats not the case, or you need to be more selective.

--- OrigNames -----> NewNames
11 - VarName - 4 --> VarName - 11 - 04
01 - VarName - 6 --> VarName - 01 - 06
12 - VarName ------> VarName - 12 - 01
99 - VarName ------> VarName - 99 - 01
45 - VarName - 67 -> Unaffected (too many digits at $EndFileName)
truth
 
Posts: 221
Joined: Tue Jun 25, 2013 3:39 am
Location: Earth, OrionArm, MilkyWay


Return to Regular Expressions