Use-Cases for Regular Expression in PowerFx

Use-Cases for Regular Expression in PowerFx

Note the images used in this article were generated using the Dall-E 3 Image Generation Not the cover photo though. Does anyone know if PowerPoint can enhance image quality?

I wrote a post a few weeks ago about supercharging Match() functions with Regular Expression in PowerFx. This article goes into some detail as to how you can use regular expression patterns, predefined pattern enumerators and match options to create some powerful string-matching capabilities. What we didn't have in that article were some examples of using this capability. I recommend you use Regex101.com as you follow along with this article, as this tool will help to break down some but not all of the patterns here.

Validate a new Password

We need users to set a new password as part of an onboarding process, however, we need to validate a few aspects of the input but we're only interested in this:

  • We don't want users to enter certain characters (&, % or $)

We have a sneaky label below our New password field to display a validation warning.

For our new password field, we want to add some regular expression to do the matching, we'll simply add this to the visible option of the label:

//Regex101.com friendly.
Visible = IsMatch(txtPassword.Text,"[\&\%\$]+",MatchOptions.Contains)

In this instance, we're using IsMatch because we only need a boolean response back to tell us if any disallowed characters exist in the input. We don't need to do any text extraction, so this will serve our purposes.

So now, when I enter "Password%", I get this validation warning:

Here we've used a method of text validation with Regular Expression. Using List Constructs ([ and ]) allows for creating a list of characters/sequences to either Match or with the ^ flag, not match!

[^\&\%\$]+

Validate an Email Address

Similar to the above example, we can validate an email address, but we can utilise a predefined pattern to do this:

We'll again attach the pattern match to the validation label's visible property:

//NOT Regex101.com friendly.
Visible = !IsMatch(txtEmail.Text, Match.Email)

A much easier approach using the predefined patterns in PowerFx, remember to use them where appropriate to increase the readability of your code. You can also string them together with & or Concatenate!


Conditioning and Validating OCR Data

Ever worked with OCR output? Sometimes it can be pretty horrendous to get what you need out of it. I did this a few years ago to extract both invoice numbers and vehicle registration numbers from scanned documents. Luckily we also had zonal OCR at our disposal, but we still needed some Regular Expression to validate the output. I'll use Vehicle Registrations here as an example of UK MOT Certificates with the AI Text Recogniser component:

In the above example, we want to extract the vehicle registration number from the document. We don't know where this registration will appear, but we know how to pattern the registration numbers of most UK vehicles:

Old UK: W### WWW

New UK: WW## WWW

Northern Ireland: WWW ####

With this information, we can craft a Regular Expression pattern as such:

//Regex101.com friendly.
(?<Registration>[A-Z]{1,2}\d{2,3}\s?[A-Z]{3}|[A-Z]{3}\s?\d{4})

In this example, we're using a matching group. This means any Match() function we use will return "Registration" as an option to reference the matched text in the group. We're hoping for any of the combinations of UK Licence Plates above to match.

On our page, we're using a Gallery to display all the text recogniser results, with the subtitle label being used to identify when a Registration Number is recognised by the pattern:

Text = If(
    IsMatch(
        ThisItem.Text,
        "(?<Registration>[A-Z]{0,2}\d{2,3}\s?[A-Z]{3}|[A-Z]{3}\s?\d{4})"
    ),
    "Registration Found",
    "No Registration Found"
)

We can likewise set the value of the identified registration to a variable with a button click in the gallery:

OnSelect = Set(
    gvRegistration,
    Match(
        ThisItem.Text,
        "(?<Registration>[A-Z]{0,2}\d{2,3}\s?[A-Z]{3}|[A-Z]{3}\s?\d{4})"
    ).Registration
)

You could also incorporate a similar strategy to find invoice numbers in documents, or reference numbers in lots of raw data. The Or operator (| Pipe) allows us to conditionally select more than one pattern in a matching group.


Splitting copied Excel data into records

Sometimes we want a quick way of processing input data, we don't necessarily want to change or modify it, but we can do things to help us work with it more effectively.

In this instance, we want to split copied data from Excel into a single-column table.

We have a Canvas App configured with a Multiline Text input and a Vertical Gallery to display the results.

The formula in the Items gallery will look something like this:

Split(TextInput1.Text,Match(TextInput1.Text,"\s+").FullMatch)

Matching "\s+" means we'll match any whitespace in the data, including line breaks. We can use \n to be more specific about returning line breaks. The plus means we'll match as many times as we need to consecutively. Excel data is copied to the text box and...

Regular Expression matches strings, but the matches that can be returned can help towards some more powerful functionality in your apps and services.


Thanks for reading this article on some examples of using Regular Expression in PowerFx. I hope you enjoyed this article and had fun following along with some of the examples given.

I'm interested to see how you're using Regular Expression in your projects, or if you think Regular Expression may help you achieve a desired outcome.