如何正则表达式 Zapier 并获得输出? [英] How to regex Zapier and get output?

查看:22
本文介绍了如何正则表达式 Zapier 并获得输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常收到来自同一个人的电子邮件,每封电子邮件都包含一个或多个唯一的识别代码.我需要得到那些代码.

电子邮件正文包含大量不一致的电子邮件内容,但它是我感兴趣的字符串.它们看起来像......

  • loYm9vYzE6Z-aaj5lL_Og539wFer0KfD
  • FuZTFvYzE68y8-t4UgBT9npHLTGmVAor
  • JpZDRwYzE6dgyo1legz9sqpVy_F21nx8
  • ZzZ3RwYzE63P3UwX2ANPI-c4PMo7bFmj

字符串似乎有一个共同点,它们的长度都是 32 个字符,并且都由大写、小写、数字和符号的混合组成.但是给定的电子邮件可能不包含、一个或多个,并且字符串将处于不可预测的位置,而不是如上所示的相邻行.

我希望在

我需要使用代码框构造一个正则表达式来查找每个唯一标识符字符串并将其作为输入输出到工作流中的下一个集成,即.特雷洛.

有关信息,在上面的屏幕截图中,框中现有的hello world"代码是 Zapier 自己的测试代码.字段id"和hello"可供链中的下一个工作流应用使用.

但是我需要对在电子邮件正文中找到的所有字符串进行处理 - 即.如果一封电子邮件只包含一个代码,请创建一张 Trello 卡片;但如果一封电子邮件包含四个代码,请为这四个代码中的每一个创建一张 Trello 卡片.

也就是说,可能有多个输出.我不知道这是如何工作的,因为我认为这些工作流程应该只适应一个操作.

我需要一些帮助才能翻山越岭.谢谢.

解决方案

这里是 David,来自 Zapier 平台团队.

很高兴您对代码步骤感兴趣.假设您的假设(正好 32 个字符)总是正确的,这应该是相当简单的.

首先,正则表达式.我们想要寻找一个字符,它是一个字母、数字或标点符号.幸运的是,javascript 的 \w 等效于 [A-Z0-9a-z_],它涵盖了除 -,我们将手动包含.最后,我们想要正好 32 个字符长度的字符串,所以我们会要求它.我们还想添加 global 标志,以便我们找到所有匹配项,而不仅仅是第一个.所以我们有以下内容:

/[\w-]{32}/g

您已经介绍了身体的映射,所以很好.javascript代码如下:

//存储任意长度(0 或更多)的数组以及匹配项var 匹配 = inputData.body.match(/[\w-]{32}/g)//.map 函数为每个函数执行一次无名内部函数//数组的元素并返回一个带有结果的新数组//[{str: 'loYm9vYzE6Z-aaj5lL_Og539wFer0KfD'}, ...]return (匹配 || []).map(function (m) { return {str: m} })

在这里,您将利用代码步骤的一个未公开的特性:当您返回一个对象数组时,后续步骤对每个对象执行一次.如果您返回一个空数组(如果没有找到键就会发生这种情况),zap 将停止并且没有其他任何事情发生.当您进行测试时,除了第一个结果之外,没有任何迹象表明任何事情都起作用.一旦您的 zap 开启并真正运行,它就会按照此处的描述散开.

仅此而已!希望这一切都有意义.如果您有任何其他问题,请告诉我!

I regularly receive emails from the same person, each containing one or more unique identifying codes. I need to get those codes.

The email body contains a host of inconsistent email content, but it is the strings I am interested in. They look like...

  • loYm9vYzE6Z-aaj5lL_Og539wFer0KfD
  • FuZTFvYzE68y8-t4UgBT9npHLTGmVAor
  • JpZDRwYzE6dgyo1legz9sqpVy_F21nx8
  • ZzZ3RwYzE63P3UwX2ANPI-c4PMo7bFmj

What the strings seem to have in common is, they are all 32 characters in length and all composed of a mixture of both uppercase, lowercase, numbers and symbols. But a given email may contain none, one or multiple, and the strings will be in an unpredictable position, not on adjacent lines as above.

I wish to make a Zap workflow in Zapier, the linking tool for web services, to find these strings and use them in another app - ie. whenever a string is found, create a new Trello card.

I have already started the workflow with Zapier's "Gmail" integration as a "trigger", specifically a search using the "from:" field corresponding to the regular sender. That's the easy part.

But the actual parsing of the email body is foxing me. Zapier has a rudimentary email parser, but it is not suitable for this task. What is suitable is using Zapier's own "Code" integration to execute freeform code - namely, a regular expression to identify those strings.

I have never done this before and am struggling to formulate working code. Zapier Code can take either Python (documentation) or Javascript (documentation). It supports data variables "input_data" (Python) or "inputData" (Javascript) and "output" (both).

See, below, how I insert the Gmail body in to "body" for parsing...

I need to use the Code box to construct a regular expression to find each unique identifier string and output it as input to the next integration in the workflow, ie. Trello.

For info, in the above screengrab, the existing "hello world" code in the box is Zapier's own test code. The fields "id" and "hello" are made available to the next workflow app in the chain.

But I need to do my process for all of the strings found within an email body - ie. if an email contains just one code, create one Trello card; but if an email contains four codes, create a Trello card for each of the four.

That is, there could be multiple outputs. I have no idea how this could work, since I think these workflows are only supposed to accommodate one action.

I could use some help getting over the hill. Thank-you.

解决方案

David here, from the Zapier Platform team.

I'm glad you're showing interest in the code step. Assuming your assumptions (32 characters exactly) is always going to be true, this should be fairly straightforward.

First off, the regex. We want to look for a character that's a letter, number, or punctuation. Luckily, javascript's \w is equivalent to [A-Z0-9a-z_], which covers the bases in all of your examples besides the -, which we'll include manually. Finally, we want exactly 32 character length strings, so we'll ask for that. We also want to add the global flag, so we find all matches, not just the first. So we have the following:

/[\w-]{32}/g

You've already covered mapping the body in, so that's good. The javascript code will be as follows:

// stores an array of any length (0 or more) with the matches
var matches = inputData.body.match(/[\w-]{32}/g) 

// the .map function executes the nameless inner function once for each 
// element of the array and returns a new array with the results
// [{str: 'loYm9vYzE6Z-aaj5lL_Og539wFer0KfD'}, ...]
return (matches || []).map(function (m) { return {str: m} })

Here, you'll be taking advantage of an undocumented feature of code steps: when you return an array of objects, subsequent steps are executed once for each object. If you return an empty array (which is what'll happen if no keys are found), the zap halts and nothing else happens. When you're testing, there'll be no indicator that anything besides the first result does anything. Once your zap is on and runs for real though, it'll fan out as described here.

That's all it takes! Hopefully that all makes sense. ​Let me know if you've got any other questions!

这篇关于如何正则表达式 Zapier 并获得输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆