• HashiCorp Developer

  • HashiCorp Cloud Platform
  • Terraform
  • Packer
  • Consul
  • Vault
  • Boundary
  • Nomad
  • Waypoint
  • Vagrant
Packer
  • Install
  • Tutorials
  • Documentation
  • Guides
  • Plugins
  • Try Cloud(opens in new tab)
  • Sign up
Packer Home

Documentation

Skip to main content
  • Documentation
  • HCP Packer

  • Terminology
    • Overview
      • Overview
        • Overview
          • chomp
          • format
          • formatlist
          • indent
          • join
          • lower
          • replace
          • regex_replace
          • regex
          • regexall
          • split
          • strrev
          • substr
          • title
          • trim
          • trimprefix
          • trimsuffix
          • trimspace
          • upper
      • Variables
      • Locals
      • Contextual Variables
      • Data Sources
      • Path Variables
      • Syntax
      • Only Except
      • Expressions
      • JSON Syntax


  • Installing Packer
  • Configuring Packer

  • Integration Program

  • Debugging

  • Resources

  • Tutorial Library
  • Community Forum
    (opens in new tab)
  • Support
    (opens in new tab)
  • GitHub
    (opens in new tab)
  1. Developer
  2. Packer
  3. Documentation
  4. Templates
  5. HCL Templates
  6. Functions
  7. String Functions
  8. regex_replace
  • Packer
  • v1.7.x
  • v1.6.x
  • v1.5.x

ยปregex_replace Function

regex_replace searches a given string for another given substring, and replaces each occurrence with a given replacement string. The substring argument can be a valid regular expression or a string.

regex_replace(string, substring, replacement)

substring should not be wrapped in forward slashes, it is always treated as a regular expression. The replacement string can incorporate captured strings from the input by using an $n or ${n} sequence, where n is the index or name of a capture group.

Examples

> regex_replace("hello world", "world", "everybody")
hello everybody


> regex_replace("hello world", "w.*d", "everybody")
hello everybody

> regex_replace("-ab-axxb-", "a(x*)b", "$1W)
---

> regex_replace("-ab-axxb-", "a(x*)b", "${1}W")
-W-xxW-

Related Functions

  • replace searches a given string for another given substring, and replaces all occurrences with a given replacement string.
Edit this page on GitHub

On this page

  1. regex_replace Function
  2. Examples
  3. Related Functions
Give Feedback(opens in new tab)
  • Certifications
  • System Status
  • Terms of Use
  • Security
  • Privacy
  • Trademark Policy
  • Trade Controls
  • Give Feedback(opens in new tab)