python正则替换字符串中间字符表达式怎么写,python字符串,import resrc


import resrc = 'aabcdddd'reobj = re.compile('ab(.*)d')src1 = reobj.sub('e', src)print src1

我的意图是替换 ab和d 中间的字符, 也就是正则的字串,而不是把 ab(.*)d 都替换

python是如何完成这项工作的呢

import resrc = 'aabcdddd'print re.sub( '(ab).*(d)', '\\1e\\2', src )

我想你应该换个思路 ...

src  = 'aabcdddd'rgx = re.compile('(?<=ab).*?(?=d)')print rgx.sub('e',src)

编橙之家文章,

评论关闭