如何从 Power Automate 中的随机文本中提取一串数字?

How do I extract a string of numbers from random text in Power Automate?

我正在设置一个流程来组织电子邮件并将其以 PDF 格式保存在 Dropbox 文件夹中。将到达的第一封电子邮件包含一个 10 位数字的标识号,我将其与地址一起提取。我的流程在 Dropbox 中创建了一个以这种格式命名的文件夹:2023568684 : 123 Main St。几周后,我需要将其他电子邮件放入该文件夹。主题中始终有一个 10 位数字。我围绕每封电子邮件进行构建,并使用 splitfirstlast 等函数来隔离 10 位数字 ID。问题是消息的主题或正文不一致,无法使用该方法轻松找到 ID。我最终开始单独构建每种电子邮件格式,但数量太多了,更不用说新发件人或格式更改的可能性了。

我的想法是在收到新邮件时使用 List files in folder,这将创建一个数组,我可以 filter 找到该邮件需要保存到的文件夹 ID。我知道由于 20 个文件的限制而对此有限制,但这是一个不同的主题和问题。

现在,如何在随机格式的电子邮件主题行中找到一个随机的 10 位数字,以便我可以将其与 filter 函数一起使用?

对于这个要求,你确实需要正则表达式,目前,PowerAutomate 不支持使用正则表达式,但好消息是它看起来即将到来......

https://powerusers.microsoft.com/t5/Power-Automate-Ideas/Support-for-regex-either-in-conditions-or-as-an-action-with/idi-p/24768

有连接器,但好像不是免费的...

https://plumsail.com/actions/request-free-license

为了暂时解决这个问题,我的建议是在 Azure 中创建一个函数应用程序并让它完成工作。这可能不是您的菜,但它会起作用。

我使用以下代码(直接在门户中)创建了一个 .NET (C#) 函数...

#r "Newtonsoft.Json"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;

public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    dynamic data = JsonConvert.DeserializeObject(requestBody);
    
    string strToSearch = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String((string)data?.Text));
    string regularExpression = data?.Pattern;

    var matches = System.Text.RegularExpressions.Regex.Matches(strToSearch, regularExpression);

    var responseString = JsonConvert.SerializeObject(matches, new JsonSerializerSettings()
    {
        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
    });

    return new ContentResult()
    {
        ContentType = "application/json",
        Content = responseString
    };
}

然后在 PowerAutomate 中调用 HTTP 操作,传入要搜索的内容的 base64 编码字符串...

这是 JSON 中的表达式 ... base64(variables('String to Search')) ... 这是您需要传入的 json ...

{
    "Text": "@{base64(variables('String to Search'))}",
    "Pattern": "[0-9]{10}"
}

这是响应的示例...

[
  {
    "Groups": {},
    "Success": true,
    "Name": "0",
    "Captures": [],
    "Index": 33,
    "Length": 10,
    "Value": "2023568684"
  },
  {
    "Groups": {},
    "Success": true,
    "Name": "0",
    "Captures": [],
    "Index": 98,
    "Length": 10,
    "Value": "8384468684"
  }
]

接下来,添加一个 Parse JSON 操作并使用此架构 ...

{
    "type": "array",
    "items": {
        "type": "object",
        "properties": {
            "Groups": {
                "type": "object",
                "properties": {}
            },
            "Success": {
                "type": "boolean"
            },
            "Name": {
                "type": "string"
            },
            "Captures": {
                "type": "array"
            },
            "Index": {
                "type": "integer"
            },
            "Length": {
                "type": "integer"
            },
            "Value": {
                "type": "string"
            }
        },
        "required": [
            "Groups",
            "Success",
            "Name",
            "Captures",
            "Index",
            "Length",
            "Value"
        ]
    }
}

最后,提取您找到的与正则表达式模式匹配的第一个值。它 returns 如果找到多个结果,所以如果你需要,你可以用这些做一些事情。

这是表达式... @{first(body('Parse_JSON'))?['value']}

从这个字符串...

We're going to search for string 2023568684 within this text and we're also going to try and find 8384468684, this should work.

...这是结果...

没有 Premium PowerAutomate 许可证所以不能使用 HTTP 操作?

您可以使用 Azure 中的 LogicApps 服务执行完全相同的操作。它是相同的引擎,但在连接器和行为方面存在一些细微差别。

使用 Azure Functions 操作代替 HTTP。

关于您在收到电子邮件时触发的操作,在 LogicApps 中,它将每隔 x seconds/minutes/hours/etc 轮询一次。而不是触发事件。我不能 100% 确定您使用的是哪个电子邮件连接器,但它应该存在。

存在 Dropbox 连接器,没问题。

您可以将 PowerAutomate 流程导出为 LogicApps 格式,这样您就不必从头开始。

https://docs.microsoft.com/en-us/azure/logic-apps/export-from-microsoft-flow-logic-app-template

如果您担心费用,请不要担心。只要确保您使用消费计划即可。只有当应用程序定期 运行 几分钟时,这些服务的成本才会真正增加。为了您自己的心理健康,请留意它。

要获取函数URL,您可以在函数本身中找到它。你必须在函数中...