简体   繁体   English

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

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

I am setting up a flow to organize and save emails as PDF in a Dropbox folder.我正在设置一个流程来组织电子邮件并将其保存为 Dropbox 文件夹中的 PDF。 The first email that will arrive includes a 10 digit identification number which I extract along with an address.将到达的第一个 email 包括一个 10 位数的标识号,我将其连同地址一起提取。 My flow creates a folder in Dropbox named in this format: 2023568684: 123 Main St .我的流程在 Dropbox 中创建了一个以这种格式命名的文件夹: 2023568684: 123 Main St Over a few weeks, additional emails arrive that I need to put into that folder.几周后,我需要将其他电子邮件放入该文件夹。 The subject always has the 10 digit number in it.主题中始终包含 10 位数字。 I was building around each email and using functions like split , first , last , etc to isolate the 10 digits ID.我围绕每个 email 进行构建,并使用splitfirstlast等函数来隔离 10 位数字 ID。 The problem is that there is no consistency in the subjects or bodies of the messages to be able to easily find the ID with that method.问题是消息的主题或正文没有一致性,无法使用该方法轻松找到 ID。 I ended up starting to build around each email format individually but there are way to many, not to mention the possibility of new senders or format changes.我最终开始围绕每个 email 格式单独构建,但有很多方法,更不用说新发件人或格式更改的可能性。

My idea is to use List files in folder when a new message arrives which will create an array that I can filter to find the folder ID the message needs to be saved to.我的想法是在新消息到达时使用List files in folder这将创建一个数组,我可以filter该数组以查找需要将消息保存到的文件夹 ID。 I know their is a limitation on this because of the 20 file limit but that is a different topic and question.由于 20 个文件的限制,我知道他们对此有限制,但这是一个不同的主题和问题。

For now, How do I find a random 10 digit number in randomly formatted email subject line so I can the use it with the filter function?现在,我如何在随机格式的 email 主题行中找到一个随机的 10 位数字,以便我可以将它与filter function 一起使用?

For this requirement, you really need regex and at present, PowerAutomate doesn't support the use of regex expressions but the good news is that it looks like it's coming...对于这个要求,你真的需要正则表达式,目前,PowerAutomate 不支持使用正则表达式,但好消息是它看起来即将到来......

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

There is a connector but it looks like it's not free...有一个连接器,但看起来它不是免费的......

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

To get around it for now, my suggestion would be to create a function app in Azure and let it do the work.为了暂时解决它,我的建议是在 Azure 中创建一个 function 应用程序并让它完成工作。 This may not be your cup of tea but it will work.这可能不是你的一杯茶,但它会起作用。

I created a .NET (C#) function with the following code (straight in the portal)...我使用以下代码(直接在门户中)创建了 .NET (C#) function ...

#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
    };
}

Then in PowerAutomate, call the HTTP action passing in a base64 encoded string of the content you want to search...然后在 PowerAutomate 中,调用 HTTP 操作,传入要搜索的内容的 base64 编码字符串...

HTTP

The is the expression in the JSON... base64(variables('String to Search')) ... and this is the json you need to pass in...这是 JSON... base64(variables('String to Search'))中的表达式...这是您需要传入的 json...

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

This is an example of the response...这是响应的示例...

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

Next, add a Parse JSON action and use this schema...接下来,添加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"
        ]
    }
}

解析 JSON

Finally, extract the first value that you find which matches the regex pattern.最后,提取您找到的与正则表达式模式匹配的第一个值。 It returns multiple results if found so if you need to, you can do something with those.如果找到,它会返回多个结果,因此如果需要,您可以对这些结果进行处理。

多变的

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

From this string...从这个字符串...

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

... this is the result... ……这就是结果……

结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM