Extract Email address using Custom Code Connector

Recently, I did a session about custom code connectors for Global Power Boot Camp. One of the demos was about extracting email addresses using Power Automate. After the demo, some of the attendees reached out to me requesting a code sample for the demo and wanted to know more about the “Code (Preview)” demo.

In this blog, I will be sharing code samples and step-by-step tutorials on how we can extract email addresses using Regular expressions in Power Automate (you can use the concept and build your own custom connector). However, before we begin, I would highly recommend you have a look at the Microsoft Documentation to watch my video Tutorials to wrap your head around the basics.

Swagger file –

swagger: '2.0'
info: {title: GBBP Demo, description: Extract Email address from String, version: '1.0'}
host: clavin.net
basePath: /
schemes: [https]
consumes: []
produces: []
paths:
  /ExtractEmail:
    post:
      responses:
        default:
          description: default
          schema: {}
      summary: This action will extract Emails from String
      description: This action will extract Emails from String
      operationId: ExtractEmail
      parameters:
      - name: body
        in: body
        required: false
        schema:
          type: object
          properties:
            textToScrape: {type: string, description: textToScrape}
definitions: {}
parameters: {}
responses: {}
securityDefinitions: {}
security: []
tags: []

Sample Code

public class Script : ScriptBase
{
    public override async Task<HttpResponseMessage> ExecuteAsync()
    {
        var contentAsString = await this.Context.Request.Content.ReadAsStringAsync().ConfigureAwait(false);
        
        var contentAsJson = JObject.Parse(contentAsString);
        var textToScrape = (string)contentAsJson["textToScrape"];

        Regex reg = new Regex(@"[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}", RegexOptions.IgnoreCase);
        Match match;

        List<string> results = new List<string>();
        for (match = reg.Match(textToScrape); match.Success; match = match.NextMatch())
        {
            if (!(results.Contains(match.Value)))
            results.Add(match.Value);
        }

        string combinedString = string.Join( ",", results);

        HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
        response.Content = new StringContent(combinedString);
        return response;
    }
}

Test your custom connector

All Done – Create a Power Automate(Flow), add the “Custom Connector” to your Power Automate.

Subscribe to this blog for the latest updates about SharePoint Online, Microsoft Flow, Power Apps, and document conversion and manipulation.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s