How to find and replace an array of strings from a large string variable using PowerFx?

Summary

To remove the clutter of the requirement, let’s reproduce the problem here. I was given a large string variable. The value of the string had the following values. The last “99999999” values are the digits and they can be any value and any repetition.


https://uts\-ws.nlm.nih.gov/rest/content/2023AA/source/SNOMEDCT_US/99999999

Note: There was a Hyphen character, so I had to add the escape for it like '\-'

The task was to replace the above string with the following string.

http://snomed.info/sct

One mistake every developer will make is that the ForAll in PowerFx will treat like ForEach. There is no ForEach in PowerFX.

Also, in the above find string, I need to make use of the Regular Expression to find the digits using the MatchAll.

Solution

I will first show you the entire solution code and explain the working of it.

/// The following is a record in some table... 
/// Assume the large value is in the "FHIRBundleObject" variable of type JOSN object.
/// Running JSON function on this variable returns a large string.
{
  sourceType: "inline",
  value: With (
    //// First part of With Operator
    {
      matches: MatchAll(
        JSON(FHIRBundleObject),
        "https://uts\-ws.nlm.nih.gov/rest/content/2023AA/source/SNOMEDCT_US/\d+",
        MatchOptions.IgnoreCase
      )
    }
    ,
    //// Second part of With operator
    Collect(
      labelTextColl,
      JSON(FHIRBundleObject)
    );
    ForAll(
      matches,
      Collect(
        labelTextColl,
        Substitute(
          Last(labelTextColl).Value,
          ThisRecord.FullMatch,
          "http://snomed.info/sct"
        )
      )
    );
    Last(labelTextColl).Value; // Very Important as the results returned
  ) // End of With
}

Assuming you are aware of how the PowerApps makes use of Tables and Records, I have reduced the code to start with a sample record. The first field is “sourceType”, which is just a hard-coded value. The second field “value” starts with the With Operator.

In this With’s initial record setting is to search for the RegEx expression using MatchAll. Notice there is “\d+” which does the job to find any digits with any repetitions. The matching values now are collected in the “matches” collection.

In the second part of With operator, we first convert the passed “FHIRBundleObject” variable as a large string using the JSON function.

Now iterate over the “matches” collection to find and replace the string using the Substitute function. The trick here is we will use the same collection i.e. “labelTextColl” and its Last index Value. Collect always add a new value at the end of the index. In this technique, we are always applying to replace to the last index value and adding the new value in the loop.

Conclusion

I hope this tip is useful to you in your scenario. I know it may be simple but if you want to do something similar using ForAll do something with a large string for a list of other collections such as a list of words to search. Just copy this code and make use of it. Thank you!

About Pankaj

I am a Developer and my linked profile is https://www.linkedin.com/in/pankajsurti/
This entry was posted in Power Apps. Bookmark the permalink.

1 Response to How to find and replace an array of strings from a large string variable using PowerFx?

  1. Pingback: How to create a JSON string array in PowerApps? | Pankaj Surti's Blog

Leave a comment