Terrafrom used RE2 regex engine, and the regex
function returns only the captured value if you define a capturing group in the regex pattern. It will return a list of captures if you have more than one capturing group in your pattern, but here, you need just one.
To extract all text inside parentheses:
> regex("[(]([^()]+)[)]", "Mark Johnson (mark@johnson.com)")
The [(]
matches a (
char, ([^()]+)
captures into Group 1 any one or more chars into Group 1, and [)]
matches a )
char.
To extract an email-like string from parentheses:
> regex("[(]([^()@[:space:]]+@[^()[:space:]]+[.][^()[:space:]]+)[)]", "Mark Johnson (mark@johnson.com)")
Here, [^()@[:space:]]+
matches 1 or more chars other than (
, )
, @
and whitespace.
See the regex demo