Python regex for selecting multiple lines of code

0 votes

I want to develop a Python Regex that can choose a complete Terraform resource block. I want to choose each resource block separately when there are several of them in a file (see example below).

The following regexes have been tested. When there are numerous closing brackets in the code, the first one becomes tangled. The second one simply chooses the entire file.

1) match = re.search(r'resource.*?\{(.*?)\}', code, re.DOTALL)
2) match = re.search(r'resource.*?\{(.*)\}', code, re.DOTALL)

Sample file:

resource "aws_s3_bucket_notification" "aws-lambda-trigger" {
  bucket = aws_s3_bucket.newbucket.id
  lambda_function {
    lambda_function_arn = aws_lambda_function.test_lambda.arn
    events              = ["s3:ObjectCreated:*"]
    filter_prefix       =  var.prefix
    filter_suffix       =  var.suffix
  }
}
resource "aws_s3_bucket" "newbucket" {
    bucket = var.bucket_name
    force_destroy = true
    acl = var.acl_value
}

Jan 24, 2023 in AWS by Tejashwini
• 3,820 points
258 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In AWS

0 votes
1 answer

How to build a Python project for a specific version of Python?

To build a Python project for a ...READ MORE

answered Feb 17, 2023 in AWS by anonymous
251 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Get all the read more links of amazon.jobs with Python

As you've noticed your request returns only ...READ MORE

answered Sep 28, 2018 in AWS by Priyaj
• 58,090 points
1,153 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,069 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP