Matching Numeric Ranges with a Regex – Logic Apps

Posted: January 9, 2023  |  Categories: Azure BizTalk

XML Validation is one of the cornerstones of BizTalk Solutions. In my last blog I show how to do this in Azure Logic Apps. This blog highlights a special case, matching numeric ranges with a regex.

The Problem

Migrating BizTalk solutions to Azure sometimes unearth poor implementations of business rules that require fixing. For example, consider this Scripting functoid in a BizTalk map.

public string OutputReceiverCode(string ReceiverCode, string CusRef2)
{
	string RcvrCode = String.Empty;
	
	if (String.IsNullOrWhiteSpace(ReceiverCode))
	{
		ReceiverCode = String.Empty;
	}

	if (String.IsNullOrWhiteSpace(CusRef2))
	{
		CusRef2 = String.Empty;
	}
	
if (ReceiverCode != "NZDF")
	{
		RcvrCode = ReceiverCode;
	}
else
	{
		Int64 POnum;
		if (Int64.TryParse(CusRef2, out POnum))
		{
			if ( (POnum > 2999999999) && (POnum < 3100000000) ||
			     (POnum > 3499999999) && (POnum < 3600000000)    )
			{
				RcvrCode = "customer";
			}
			else
			{
				RcvrCode = "customerInvError";
			}
		}  
		else
		{
			RcvrCode = "NZDFInvError";
		}
	}
	return RcvrCode;
}

The code is unremarkable and checks the PO Number to see if it is a particular range 29999999993100000000 or 34999999993600000000. After the map is run, if the DestinationId is ‘customer’ then the normal invoice processing occurs. Conversely, if the DestinationId is ‘NZDFInvError’ an error reporting ensues.

While this works it is poor practice to be validating like this in a map. Furthermore, reproducing this in the Logic App was going to be problematic. Thus, we choose to validate outside of the map, using XML schema validation instead. I did not find many worked examples and show my solution here.

Matching Numeric Ranges with a Regex

In my previous blog the PO number had to be in one range only. Thus, a simple Schema restriction with a maxInclusive and minInclusive was sufficent.

<xs:attribute name="BuyerOrderRef" use="optional">
<xs:simpleType>
<xs:restriction base="xs:float">
<xs:maxInclusive value="9999999999"/>
<xs:minInclusive value="1000000000"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>

This time we have two ranges to consider. For this reason my solution was to use a regex pattern to validate the BuyerOrderRef XML tag.

<xs:attribute name="BuyerOrderRef" use="optional">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([2][9][9][9][9][9][9][9][9][9]|[3][0][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]|[3][1][0][0][0][0][0][0][0][0]|[3][4][9][9][9][9][9][9][9][9]|[3][5][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]|[3][6][0][0][0][0][0][0][0][0])"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>

One must be careful crafting the regex and I enlarge below for any novices. The regex is broken into separate pieces using the or character (|)

  1. [2][9][9][9][9][9][9][9][9][9], match on 2999999999.
  2. [3][0][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9], match on 3000000000 – 3099999999.
  3. [3][1][0][0][0][0][0][0][0][0], match on 3100000000.
  4. [3][4][9][9][9][9][9][9][9][9], match on 3499999999.
  5. [3][5][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9], match on 3500000000 – 3599999999.
  6. [3][6][0][0][0][0][0][0][0][0], match on 3600000000.

Putting it all together in a Logic App

Finally, how do you use this in a Logic App? Add an XML validation action that references your XML schema to the Logic App. This is shown in my previous blog.

In summary we have shown more complex example of XML schema validation that uses regex pattern matching.

turbo360

Back to Top