Regular expressions are basically used in two main scenarios:
Regular expressions are now supported only for the response body. Adding the feature to the request is planned.
Regular expressions are a really strong tool for text manipulation. You can find a basic guideline at https://regexone.com or a nice tool for testing your regular expressions at https://regex101.com.
Let's say your API returns the following JSON:
{
"id": 3,
"no": "10000",
"name": "The Cannon Group PLC",
"address": "192 Market Square",
"city": "Birmingham",
"countryCode": "GB",
"phones": []
}
But there's a catch: someone entered Great Britain with the UK
country code in your Business Central database and it's not easy to change that now. Of course, if you try to save the GB
code into a Customer record and the country does not exist, the operation ends with an error.
Regular expressions to the rescue! Select your operation from the list and open the response body schema. Then select the countryCode
node and open the node Details:
Now fill in the regular expression search and replace patterns to following fields:
"GB"
string – but regular expression patterns can be really complex, too.So if in this example the API returns country code US
, nothing happens, because there is no match with the search regex pattern. But before RESTwithUS attempts to save the data into Business Central, every GB
country code is changed to UK
.
Let's have a bit more advanced example. Say you want to get the house number from theaddress
node and save it to some variable for further use. In such case select the address
node in the response and open the node Details again. Then have a look at Variables tab:
"^[^\d]*(\d+)[^\d]*$"
will save the numbers from the string to a match group – it's this part of expression: "(\d+)"
and "\d"
is a shorcut for any number."$1"
.After you run the operation, you can check the results in Batch Entries. As you can see, there is a new line with the variable houseNumber
and it contains the house number cut from the address: