We have a few scenarios where you need to use regular expressions for Logic Apps. There really are a couple of choices:

  1. Implement some inline code each time you want to do a regex
  2. Use a 3rd party action

Its a little easier in Logic App Standard to use inline code as you need an integration account in consumption, but either way you need to write a bit of annoying code in javascript for every time you want to do a regex.

Over time we have developed a set of common helper API’s in APIM which give us useful widgets we can call to act as our own actions. We could use functions, but we try to minimize the number of function apps we spin up to minimize the additional infrastructure setup but we we already have APIM in place, adding a new API and using it from a Logic App is really simple.

The way we implement it is to call APIM from Logic Apps, the APIM policy then executes a small code snippet in the policy and returns the result.

We can call the helper api like in the below example.

Below is the policy we use, hopefully this is useful for someone else. What helper use cases do you use?

        <base />
        <set-variable name="body" value="@(context.Request.Body.As<string>(preserveContent:true))" />

        <set-variable name="pattern" value="@(context.Request.Headers.GetValueOrDefault("Pattern", ""))" />

        <set-variable name="replaceWith" value="@(context.Request.Headers.GetValueOrDefault("ReplaceWith", ""))" />

        <set-variable name="responseMessage" value="@{
            var input = (string)context.Variables["body"];
            var pattern = (string)context.Variables["pattern"];
            var replaceWith = (string)context.Variables["replaceWith"];

            string response = System.Text.RegularExpressions.Regex.Replace(input, pattern, replaceWith);
		    return response;
        }" />
            <set-status code="200" reason="Ok" />
            <set-header name="Content-Type" exists-action="override">
            <set-body template="none">@((string)context.Variables["responseMessage"])</set-body>
        <base />
        <base />
        <base />


Buy Me A Coffee