How to Use Regular Expressions in Microsoft Power Virtual Agents With Examples

Have you used RegEx in your PVA bots? Check out this post where I give patterns to the most common validations.

Regular Expressions in Power Virtual Agents? Sounds like a pretty advanced topic. But it’s actually not that difficult and can save you hours of time if you’re trying to validate user input for things such as credit card numbers, tracking IDs, custom invoice numbers or even IP addresses. In this post we’ll cover some of the basics of Regular Expression syntax so you can get started using them inside Power Virtual Agents.

Summary

To utilize regular expressions inside Power Virtual Agents, we must first create a new entity.
This can be be done by clicking the Entities tab > New entity.

Now select Regular expression (Regex)

PVA does a great job in providing some general use case examples.

The syntax is based on .NET


RegEx Examples in PVA

Below you will find some examples you can copy and paste directly into the Pattern for your Regular Expression:

PatternDescription
^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$IP Address
– Looks for X.X.X.X format
– Each X range in 0-255
– X length 0-3
^4[0-9]{12}(?:[0-9]{3})?$Visa Credit Card numbers
– Start with a 4
– Old cards use 13 digits
– New cards use 16 digits
^3[47][0-9]{13}$American Express
– Starts with 34 OR 37
– All have 15 digits
 ^(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}$Mastercard
Starts with either:
51-55 OR 2221-2720
– All have 16 digits
^(?!0{3})(?!6{3})[0-8]\d{2}-(?!0{2})\d{2}-(?!0{4})\d{4}$Social Security Number
– SSN are 9 digits
– Looks for XXX-XX-XXXX format
– Cannot contain all zeros
– Cannot begin with 666 OR 900-999
^[a-fA-F0-9]{2}(:[a-fA-F0-9]{2}){5}$Mac Address
– 6 byte hex separated by colon “:” OR dash “-”
^((6553[0-5])|(655[0-2][0-9])|(65[0-4][0-9]{2})|(6[0-4][0-9]{3})|([1-5][0-9]{4})|([0-5]{0,5})|([0-9]{1,4}))$Port Number
– Matches valid port number in computer network
– 16 bit
– Ranges from 0-65535
[A-Z]{2,}-\d+Jira Ticket Number
– Looks for format Hyphen-separated Jira project key and ticket issue number
^(bc1|[13])[a-zA-HJ-NP-Z0-9]{25,39}$Bitcoin Address
26-35 alphanumeric characters
– Start with 1 OR 3 OR bc1
^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$UUID / {guid}
– 36 characters
– 128 bit, represented in 16 octets
– Looks for format form of  8-4-4-4-12

Using them in PVA

Once we create the Entity, and define the pattern for our RegEx. We can now use this validation inside our PVA chat.

For example, I will test the IP Address pattern

^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$

I have created a topic for testing my RegEx.

To use the newly created entity, Add a Question, under Identify select your Custom Entity

Under this, I add a message to confirm its valid.
(Note, the bot will automatically let the user know if the validation does not match)

Testing the RegEx

Okay, drumroll….
The values I will be testing are

User InputValid?
192.168.1.1Valid ✔
127.0.0.1Valid ✔
999.55.1.5Not Valid ✖
Not A IP AddressNot Valid ✖

Conclusion

Being able to use Regular Expressions inside Power Virtual Agents can be extremely powerful. And with the above list of common patterns, I hope you find value in this post.

Thank you, and have a great day!