sách gpt4 ăn đã đi

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

In lại 作者:太空狗 更新时间:2023-10-30 02:43:21 26 4
mua khóa gpt4 giày nike

我有以下正则表达式:

pattern = '^[a-zA-Z0-9-_]*_(?P[A-Z]\d\d)_T\d{4}(?PF\d{3})L\d{2}A\d{2}(?PZ\d{2})(?PC\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

câu trả lời hay nhất

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

nhập khẩu lại
from functools import partial

pattern = '^[\w-]*_(?P[A-Z]\d{2})_T\d{4}(?PF\d{3})L\d{2}A\d{2}(?PZ\d{2})(?PC\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)

đầu ra:

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

在线实现可用đây

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

26 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress