2020-03-10 11:01:18 -04:00
|
|
|
---
|
2020-03-18 18:46:47 -04:00
|
|
|
layout: docs
|
2020-06-05 13:17:18 -04:00
|
|
|
page_title: regex_replace - Functions - Configuration Language
|
|
|
|
sidebar_title: regex_replace
|
2020-03-10 11:01:18 -04:00
|
|
|
description: |-
|
2020-06-05 13:17:18 -04:00
|
|
|
The regex_replace function searches a given string for another given substring,
|
2020-03-10 11:01:18 -04:00
|
|
|
and replaces all occurrences with a given replacement string. The substring
|
|
|
|
argument can be a valid regular expression or a string.
|
|
|
|
---
|
|
|
|
|
2020-06-05 13:17:18 -04:00
|
|
|
# `regex_replace` Function
|
2020-03-10 11:01:18 -04:00
|
|
|
|
2020-06-05 13:17:18 -04:00
|
|
|
`regex_replace` searches a given string for another given substring, and
|
2020-03-10 11:01:18 -04:00
|
|
|
replaces each occurrence with a given replacement string. The substring
|
|
|
|
argument can be a valid regular expression or a string.
|
|
|
|
|
|
|
|
```hcl
|
2020-06-05 13:17:18 -04:00
|
|
|
regex_replace(string, substring, replacement)
|
2020-03-10 11:01:18 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
`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
|
|
|
|
|
2020-05-29 17:12:05 -04:00
|
|
|
```shell-session
|
2020-06-05 13:17:18 -04:00
|
|
|
> regex_replace("hello world", "world", "everybody")
|
2020-03-10 11:01:18 -04:00
|
|
|
hello everybody
|
|
|
|
|
|
|
|
|
2020-06-05 13:17:18 -04:00
|
|
|
> regex_replace("hello world", "w.*d", "everybody")
|
2020-03-10 11:01:18 -04:00
|
|
|
hello everybody
|
|
|
|
|
2020-06-05 13:17:18 -04:00
|
|
|
> regex_replace("-ab-axxb-", "a(x*)b", "$1W)
|
2020-03-10 11:01:18 -04:00
|
|
|
---
|
|
|
|
|
2020-06-05 13:17:18 -04:00
|
|
|
> regex_replace("-ab-axxb-", "a(x*)b", "${1}W")
|
2020-03-10 11:01:18 -04:00
|
|
|
-W-xxW-
|
|
|
|
```
|
|
|
|
|
|
|
|
## Related Functions
|
|
|
|
|
2020-04-01 15:15:54 -04:00
|
|
|
- [`replace`](/docs/from-1.5/functions/string/replace) searches a given string for another given
|
2020-03-10 11:01:18 -04:00
|
|
|
substring, and replaces all occurrences with a given replacement string.
|