在 Ruby 中,可以使用“Regexp.last_match(index_of_subgroup)”来引用最后一个匹配 RegExp 的子组,而无需知道保存匹配 RegExp 的变量。
我想避免在 python 中使用类似 VarName.group(1) 的东西,因为我不一定知道给定的 RegExp 匹配 Varname 是什么。
Python 中有等价的东西吗?
您可以使用 re 模块和 re.lastindex 属性以及 re.group() 方法。