更新字符串中间的数字

时间:2021-11-27 19:16:06

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".

在我的结果文件中,我在resline。如果resline=第90行,我想将第二个“1,2”改为“91,92”。

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

在perl中,我将在“——”开头的行中使用以下代码

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

How should I do this in python?

我应该如何在python中实现这一点?

1 个解决方案

#1


3  

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

您将使用re.sub,并将可调用的而不是字符串作为替换:

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

#1


3  

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

您将使用re.sub,并将可调用的而不是字符串作为替换:

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