I have the following code to change the version string inside the provider "aws" block. using re.search() and re.sub(). Is it possible to do it with just re.sub()?
def process(p, provider, version):
g = re.search(r'provider\s+"%s"\s+\{[\s\S]*version\s+=\s+"(.*)"[\s\S]*\}' % provider, p).group(1)
return re.sub(r'%s' % g, '~> ' + version, p)
p = """
provider "aws" {
region = var.region
version = "~> 2.59"
assume_role {
role_arn = "tes_role"
}
}
"""
r = process(p, "aws", "2.61")
print(r)