Skip to content

Regex to match anything in braces? #2334

Answered by BurntSushi
kaihendry asked this question in Q&A
Discussion options

You must be logged in to vote

Like pretty much all other regex engines, . matches every character except for newlines. You can make it match any character by enabling the s flag (also like in almost all other regex engines):

[andrew@frink d2334]$ cat haystack          
terraform {
  backend "s3" {
    region = "ap-southeast-1"
    key    = "01-service-quota-state.json"
    bucket = "foobar-dev-infra-tf-state"
    dynamodb_table = "foobar-dev-infra-tf-state-lock"
  }

  required_providers {
    aws = {
      source = "hashicorp/aws"
    }
  }
}
[andrew@frink d2334]$ rg -U 'backend "s3" \{(.*?)\}' haystack     
[andrew@frink d2334]$ rg -U 'backend "s3" \{((?s:.)*?)\}' haystack
2:  backend "s3" {
3:    region = "ap-south…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by BurntSushi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants