如何仅替换我的正则表达式匹配对象中的第二组?

How can I substitute only the second group in my regex match object?

我有一个大数据文件,我想轻松地替换其中的值。值名称由标签表示,即 <VALUE>,然后是它们各自的 text/numerical 数量,每个值名称都有自己的线。

我写了正则表达式模式 (r'(<VALUE>)(.*)\n)。我想替换第二组。

我写了下面的代码。

def edit_attribute():
    with open("file.txt", "w+") as file:
        file_string = file.read()
        attribute_regex = re.compile(r'(<VALUE>)(.*)\n')
        mo = attribute_regex.search(file_string)
        #replace mo.group(2)

我尝试使用 re 的 sub 方法,但如果不替换包括值名称的整行我就无法这样做。

感谢任何帮助。

您不替换捕获组。捕获组用于捕获要保留的字符串部分。然后,您可以在替换字符串中使用反斜杠后跟组号来引用此值。

newstring = re.sub(r'(<VALUE>).*', r'foo', oldstring)

将产生 '<VALUE>foo'.