开发者

Update numbers in the middle of a string

I'm looking at diff开发者_开发知识库-style output, but I need to update the line numbers. So I see:

*** 1,2 *****
Actual line 1
Actual line 2
--- 1,2 -----
Expected line 1
Expected line 2

and in my results file, I'm at resline. So if resline=line 90, I'd want to change the second "1,2" to "91,92".

In perl, I'd use the following on the line that begins "---"

s/(\d+?)/($1+$resline)/eg

How should I do this in python?


You would use re.sub and pass in a callable instead of a string as the replacement:

import re
re.sub(r'\d+?', lambda m: str(int(m.group(0))+resline), YOUR_STR)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜