Extract email from a message

Hi.

I have a template that I receive a webhook with a message that contains an email address.

I want to extract only the email to insert into a spreadsheet.

How do I do that? Is it possible to use regex in Set node?

Thanks.

Hi @lfilippi, welcome to the community!

You can indeed use regex in the Set node (and most other nodes accepting expressions), though it might not be immediately obvious how to do so.

Here’s a quick example workflow showing how:

This extracts the email from a simple multiline string:

Hope this helps!

1 Like

Thanks for your response! I think I’m doing something wrong.

I used the code ={{ /.+:\s(.+).$/gm.exec($json[“body”][“m_content”])[1] }}

And I get “not found” as a result of the expression.

This is my JSON:

{
  "headers": {
    "x-real-ip": "107.178.238.43",
    "x-forwarded-for": "107.178.238.43",
    "host": "mrmnl7rxy1h0efkbqxxr3tch.hooks.n8n.cloud",
    "x-forwarded-proto": "https",
    "x-nginx-proxy": "true",
    "connection": "close",
    "content-length": "433",
    "accept": "application/json, text/plain, */*",
    "content-type": "application/json",
    "user-agent": "axios/0.21.4"
  },
  "params": {},
  "query": {},
  "body": {
    "m_id": "[email protected]_3EB08F498D9E0EF73836",
    "m_type": "chat",
    "m_datetime": "Tue Aug 16 2022 09:47:15 GMT-0300 (Horário Padrão de Brasília)",
    "m_timestamp": 1660654035,
    "m_user": "5519",
    "m_phone": "5519",
    "m_content": "testeteste [email protected]",
    "m_cname": "Ss 19 Leandro [email protected]",
    "m_uname": "Leandro",
    "m_gname": "",
    "m_gid": "",
    "w_id": "83f39533-e7b8-418c-88ca-0389280769c8"
  }
}

My workflow:

Thanks so much for sharing your example data and workflow @lfilippi!

I noticed two things here:

  1. Your webhook node would pass on your data to each of the Date & Time nodes, so you’re essentially duplicating your data here. You might want to consider putting the Date & Time node in a sequential order to avoid this.
  2. For your email extraction, I think something like /\s+(.+)$/gm could do the job, assuming your email is always at the end of the line. I have built this using regex101.com which you might find helpful as well.

Below is an example workflow including both suggestions. I have replaced the Webhook node with a Function node to make it easier to test:

Result:

Let me know if you have any questions on this!

2 Likes

Hey @lfilippi,
I used a slightly different regex to match emails. So you’ve got two options now :grin:

{{ /([a-zA-Z0-9._-][email protected][a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gm.exec($json["body"]["m_content"])[1] }}

Here is my workflow:

2 Likes

Thanks so much @MutedJam and @marcus.

Now everything is working perfectly. :smiley:

1 Like

Hi!

Sorry for reopening this topic.

I have a small problem with this workflow.

I have 6 columns that are populated by the webhook information. 3 with numbers and 3 with strings.

In columns filled with numbers, the ’ character is inserted before the data.

Example:

Should be: 000000

Arrives in the spreadsheet: '000000

It’s a detail that makes all the difference because I use the spreadsheet for other purposes and I need to manually edit the data.

I work with webhook coming from the same source with a different service than n8n and I don’t have this problem.

Any ideas on how to resolve this? Thanks again!

Hey @lfilippi,

I believe we do this in the sheets node because Sheets will try and be clever and convert the 000000 to a number which will result in just 0 being displayed.

1 Like

Perhaps my example was not the best.

This happens with all types of information that only include numbers or dates.

Another examples:

The date should be: 28/09/2022

Arrives in the spreadsheet: '28/09/2022

The phone should be: 19988048888

Arrives at the spreadsheet: '19988048888

Thanks!

Hey @lfilippi,

Can you add Value Input Mode as an option to the sheets node and set it to User Entered and see if that helps?

1 Like