[英]Replace named group in regex match
我有以下正則表達式:
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
此處提供在線實施
要獲得所需的輸出,只需捕獲內容的開始和結束
保留。 在它之間插入---
。
找^([a-zA-Z0-9_-]*_[AZ]\\d\\d_T\\d{4}F\\d{3}L\\d{2}A\\d{2})Z\\d{2}(C\\d{2}\\.tif)$
替換: $1---$2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.