1

If i got value like

email = "Mark Johnson (mark@johnson.com)"

How can i print only word in the () and output become

mark@johnson.com

Currently I using regex but It seems doesn't work with my pattern

email = regex("x{(,)}", "var.email")

How can i solve this issue , Thanks for your help.

R.Phatter
  • 11
  • 2
  • https://stackoverflow.com/questions/42407785/regex-extract-email-from-strings – Ajay Tom George May 26 '20 at 05:59
  • That's a strange looking pattern, and doesn't even come close to what you are after. If I may suggest something, possibly a pattern like `(?<=\()[^)]+` or a bit extended with lookahead too will do the job. – JvdV May 26 '20 at 06:02

1 Answers1

3

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

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563