Categories
Bizagi Tips and Tricks

Use REGEX inside expressions

Bizagi allows you to use a regular expression (REGEX) on input text fields. So what happens if you want to use REGEX inside an expression?

Use REGEX inside an expression

var sPattern = "^[0-9]{3}$"; // --- this is the regex pattern
var sToCheck = "823"; // --- this is the text you want to check agains the pattern 
var oRg = new System.Text.RegularExpressions.Regex(sPattern);
var oMatching = oRg.Matches(sToCheck);

if (oMatching.Count > 0)
{
	CHelper.ThrowValidationAlert("A match was found");
}
else
{
	CHelper.ThrowValidationAlert("No match was found");
}

… create a Library Rule called Global_Regex:

Regex Library Rule

Add to input parameters sPattern and sInput:

Parameters

and inside the expression paste the following code:

var bMatch = false;

var oRg = new System.Text.RegularExpressions.Regex(sPattern);
var oMatching = oRg.Matches(sInput);

if (oMatching.Count > 0)
{
	bMatch = true;
}

return bMatch;

With the Global Library Rule create you can use it anywhere you need it. For example to validate an email address:

var sPattern = "^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$";
var sInput = sEmailAddress;
var bMatch = ABC.Global_Regex(Me,sPattern,sInput);
if (!bMatch)
{
	CHelper.ThrowValidationError("Incorrect Email Address format");
}

Good luck coding!