How to check if a text contains certain patterns like email address or URL
💡 Do you know that Microsoft Power Automate for desktop support regular expressions or regex? You can make use of regex for the following actions - Parse Text and Replace Text.
Problem Description
Say you have a text variable containing the following information:
Regular Expression, or regex or regexp in short, is extremely and amazingly powerful in searching and manipulating text strings, particularly in processing text files. One line of regex can easily replace several dozen lines of programming codes. Regex is supported in all the scripting languages (such as Perl, Python, PHP, and JavaScript); as well as general purpose programming languages such as Java; and even word processors such as Word for searching texts. Getting started with regex may not be easy due to its geeky syntax, but it is certainly worth the investment of your time. To learn more, please visit https://werkbook.co.
You want to extract all the URLs contained inside this text. What can you do?
Solution
To extract all the URLs from the text, we can make use of the action Parse text:
Under the parameter Text to find, we will specify the following regular expression:
(?:(?:https?|ftp|file):\\/\\/|www\\.|ftp\\.)(?:\\([-a-zA-Z0-9+&@#\\/%%=~_|$?!:,.]*\\)|[-a-zA-Z0-9+&@#\\/%%=~_|$?!:,.])*(?:\\([-a-zA-Z0-9+&@#\\/%%=~_|$?!:,.]*\\)|[a-zA-Z0-9+&@#\\/%%=~_|$])
Remember to enable the option Is regular expression. In addition, the option First occurence only allows you to specify if you want to find the first match or all of the matches if there are more than 1. In the example, all the found URLs will be stored in the output variable Matches.
💡Pro tip: Use ‘%%’ to escape the special character ‘%’. Alternatively, you can also make use of the action Escape text for regular expression.
Additional Information
Last updated on: 9 Feb 2023
Tested version(s): 2.27.177.22340
Prerequisites: None
Dependencies: None
Known issues: None
References
Nil