Skip to main content

Hi! I’m trying to set up a workflow that extracts a list of tokens and adds a comma between them. I’m trying to use the `extract pattern` filter but not having much success yet.

 

This is an example of the raw text that comes from a webhook

color: blue
id: d697daa7-a4be-4c64-8004-d811cacf29a6
label: Instagram DM
placeholder: Enter Text...
value: Instagram DM

color: orange
id: 411d0d19-11c5-48c9-bd17-c80d062da680
label: Twitter DM
placeholder: Enter Text...
value: Twitter DM

color: brown
id: 07c902df-ebef-4b4d-bdec-1b1acbfc2bfa
label: Line
placeholder: Enter Text...
value: Line

color: brown
id: 3ba6ff45-64cd-441a-8373-21b45dfa7b45
label: Slack
placeholder: Enter Text...
value: Slack

 

I’m trying to extract the `label` fields, add a comma and return this string `Instagram DM, Twitter DM, Line, Slack`. 

 

Any help would be appreciated! I have a basic understanding of regexes, but I can’t seem to figure out how to get them to work in the python format Zapier requires.

 

Try the Formatter by Zapier.

Use the Utilities.

Article: https://zapier.com/help/doc/how-use-formatter-functions

 


@ericmigi checking in to see if you were squared away here with your configuration!


@ericmigi - If you don’t mind using a code step to solve this problem, here’s a code snippet to achieve what you wanted. To use, make sure to pass the raw text in an input field called rawText.

  1. Solution with Regex

const { rawText } = inputData;
const regex = /^label\s*:\s*(.+)/gm;
const results = t];

let m;
while ((m = regex.exec(rawText)) !== null) {
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
const o, match] = m;
if (match) {
results.push(match);
}
}
return results.join(", ");
  1. Another solution, but without Regex

const { rawText } = inputData;
const results = t];

rawText.split("\n").forEach((line) => {
line = String(line).trim();

if (line.startsWith("label:")) {
const o, label] = line.split("label:");
results.push(label.trim());
}
});

return results.join(", ");