【发布时间】:2013-10-22 06:00:31
【问题描述】:
我有一个文件,模式文本类似于(1A0IA:0.42722,1AYLA:0.47152)。我想用(1A0IA,1AYLA)替换它。
我知道我可以这样做:
text 是包含(1A0IA:0.42722,1AYLA:0.47152) 的字符串
expression 1 : reduced_text = re.sub(r':\d+\.\d+\,',r',',text)
output : (1A0IA,1AYLA:0.47152)
expression 2 : reduced_text = re.sub(r':\d+\.\d+\)',r')',reduced_text)
output : (1A0IA,1AYLA)
实际上我只想在(ID:float,ID:float) 中替换模式:float,,但是存在一些包含:float, 但不在这种字符串中的文本:(ID:float,ID:float)。
是否存在可以执行以下操作的表达式?
(string1:0.42722,string2:0.47152) -> (string1,string2)
第一个 .{5} 是 string 1 ;第二个.{5} 是string 2
reduced_text = re.sub(r'\(.{5}:\d+\.\d+\,.{5}:\d+\.\d+\)',r'\(.{5}\,.{5}\)',text)
【问题讨论】:
标签: python regex python-3.x