Best answer

Help with regex...please :)

  • 14 September 2020
  • 3 replies
  • 364 views

Userlevel 1

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.

 

icon

Best answer by ikbelkirasan 9 December 2020, 16:35

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

3 replies

Userlevel 7
Badge +12

@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 = [];

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

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

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

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

return results.join(", ");

 

Userlevel 7
Badge +8

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

Userlevel 7
Badge +14

Try the Formatter by Zapier.

Use the Utilities.

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