Created
November 11, 2015 16:54
-
-
Save grational/6a7763f5df7949562e1b to your computer and use it in GitHub Desktop.
Replace a matching group with a name passed as parameter with another parameter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
from functools import partial | |
pattern = '^[a-zA-Z0-9-_]*_(?P<pos>[A-Z]\d\d)_T\d{4}(?P<fID>F\d{3})L\d{2}A\d{2}(?P<zID>Z\d{2})(?P<cID>C\d{2})\.tif$' | |
filename = '151006_655866_Z01_T0001F015L01A02Z01C03.tif' | |
def replace_closure(subgroup, replacement, m): | |
if m.group(subgroup) not in [None, '']: | |
start = m.start(subgroup) | |
end = m.end(subgroup) | |
return m.group()[:start] + replacement + m.group()[end:] | |
subgroup_list = ['pos', 'fID', 'zID', 'cID'] | |
replacement = '---' | |
for subgroup in subgroup_list: | |
print re.sub(pattern, partial(replace_closure, subgroup, replacement), filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment