Text Processingflags: g
HCL / Terraform Variable Reference
Match Terraform / HCL references like `var.name`, `local.foo`, `module.x.output`, or `data.aws_ami.ubuntu.id`.
Try it in RegexPro →Available in
Pattern
regexengine-agnostic
\b(?:var|local|module|data)\.[a-zA-Z_][\w\-]*(?:\.[a-zA-Z_][\w\-]*)* (flags: g)Raw source: \b(?:var|local|module|data)\.[a-zA-Z_][\w\-]*(?:\.[a-zA-Z_][\w\-]*)*
How it works
\b(?:var|local|module|data) anchors at one of the four namespace prefixes. \.[a-zA-Z_][\w\-]* matches the first segment after the prefix. (?:\.[a-zA-Z_][\w\-]*)* matches additional dotted accessors (deep references like `module.network.outputs.vpc_id`).
Examples
Input
vpc_id = module.network.vpc_idMatches
module.network.vpc_id
Input
tags = merge(var.common_tags, local.env_tags)Matches
var.common_tagslocal.env_tags
Input
no references hereNo match
—Common use cases
- •Terraform dependency graph extraction
- •Refactor tooling (rename a variable across all files)
- •Linting for unused variables
- •Module-boundary auditing
Related patterns
Unix Environment Variable Reference
Text ProcessingMatch Unix shell environment variable references in both $VAR and ${VAR} forms.
Terraform Resource Block Header
Text ProcessingMatch the opening line of a Terraform `resource "type" "name" {` block, capturing the resource type and local name.
JavaScript Variable Declaration
Text ProcessingMatch JavaScript / TypeScript variable declarations (`var`, `let`, `const`), capturing the keyword and identifier name.
GraphQL Operation Header
Text ProcessingMatch GraphQL operation headers — `query`, `mutation`, or `subscription` — capturing the operation name.