🐮 How to name variables in regular expressions

I learned this trick from @ludekvolejnik – a master of regular expressions :wink:
Thanks Luďa.

What is it about

So far I have shown that in Blending Bull you can parse variables from a text. Then you can use these variables in output file generation. See an example where I processed data from an XML file to CSV for import to Mergado. By a regular expression in the “Find text in a data source” field, I got two variables.

The regular expression was:
<item id="(.*?)"><stock_quantity>(.*?)<\/stock_quantity>.*<\/item>

Then I entered the following into the “Replace for text” field:
"\g<1>","\g<2>"

I marked the variables as \g<X> where X was the number of order in input text.

Problem

The described procedure works reliably if both the variables are listed in all items. I, however, came across a situation when it was not clear. And @ludekvolejnik showed me the following:

Regular expression:
(?P<name>.*?)

This corresponds to (.*?) but it has one interesting feature. You will write it into the output as:
\g<name>

which means the individual variables can be named!

Example how to generate CSV file form XML feed

Regular expression:

<item id="(?P<ID>.*?)"><stock_quantity>(?P<QUANTITY>.*?)<\/stock_quantity>.*<\/item>

Replace for text:

"\g<ID>","\g<QUANTITY>"

Solution

I consider naming variables better than order. The notation looks difficult. I will save this website among the bookmarks in the browser. And once I need the notation, I will copy it from this example :wink:

1 Like