I am just recently learning and utilizing the power of regular expressions
I have a tuple list of files returned from os.walk(), like so:
files = ('s8_00.tif', 's9_00.tif', 's10_000.tif', 's11_00.tif')
I am trying to get it to look like this:
files = ('s8_##.tif', 's9_##.tif', 's10_###.tif', 's11_##.tif')
I have tried to use this.
pad2 = re.compile(r'_00?')
for root, dirs, files in seqDirs:
pad = files[0]
p = pad2.sub("#", pad)
print p
This returns:
p = ('s8#.tif', 's9#.tif', 's10#0.tif', 's11#.tif')
So I changed the expression around to:
pad2 = re.compile('(_)0+')
giving me:
p = ('s8#.tif', 's9#.tif', 's10#.tif', 's11#.tif')
Is the problem in my p = pad2.sub function? Or is the problem exist within my compiled expression? Or is it the "_" being in the expression that is screwing it up?
I tried even passing some expression inside the pad2.sub function just to test it out and of course that didn’t really work. I know I am missing something little here and I am a bit stuck.
Any and all help will be greatly appreciated along with explanations of logic.
We’re going to use a function for the replacement, not a string.
?<=is a positive lookbehind assertion. You can find an explanation in the docs at Regular Expression Syntax.0+captures all following zerosThe lambda function replaces every
0with the#.