gpt4 book ai didi

python - 替换正则表达式匹配中的命名组

转载 作者:太空狗 更新时间:2023-10-30 02:43:21 26 4
gpt4 key购买 nike

我有以下正则表达式:

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'

与团体:

m = re.match(pattern, filename)
print m.group("pos") # Z01
print m.group("fID") # F015
print m.group("zID") # Z01

如何在 Python 中仅用给定的字符串替换指定的组?

我尝试将 re.sub 与函数调用一起使用,但不知道该函数应该是什么样子:

def replace_function(matchobj):
# how to replace only a given match group?
# (the following replaces *all* occurrences of "Z01" in this example)
return matchobj.group(0).replace(matchobj.group("slice"), "---")

print re.sub(pattern, replace_function, filename)

我想要的结果是:

151006_655866_Z01_T0001F015L01A02---C03.tif

最佳答案

您可以使用闭包和所选匹配组的开始/结束索引来执行您需要的操作:

import re
from functools import partial

pattern = '^[\w-]*_(?P<pos>[A-Z]\d{2})_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)

输出:

151006_655866_---_T0001F015L01A02Z01C03.tif
151006_655866_Z01_T0001---L01A02Z01C03.tif
151006_655866_Z01_T0001F015L01A02---C03.tif
151006_655866_Z01_T0001F015L01A02Z01---.tif

在线实现可用here

关于python - 替换正则表达式匹配中的命名组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33634232/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com