Storing regular expressions in global variables

I’m not sure if this is really an issue or not so I’ve marked it as General.

I am using a text global variable to store multiple values in a simple json type array.

eg. {s:[1,2,3,4,5,6]}

I can use the tc(json… and tc(reg… functions to manipulate this set of numbers in any way I choose using regular expressions but mostly in a bid to alter more than one value at a time which I cannot do manually with single values in global variables.

Each of the numbers in the array could be single or double figures so I would use the regex formula (\d{1,2}) six times in a row separated by a comma as seen in the array above.

Sadly I cannot use {6} to just repeat the capture formula six times as I later need to address individual values in the array, not just one big one. Besides this, the last three numbers may also be negative so the formula for them is changed to (-?\d{1,2}). These small formulas have to be wrapped in parentheses as I am also using capture groups so I can reconstruct the json array above piece by piece.

If this was all that was needed it would still be a lot, but we also have to include regex matching for the braces and square brackets at the start and beginning which regex regards as special characters which need to be ‘escaped’.

So to recognise the entire array above requires the following regex expression:-

(\{s:\[)(\d{1,2}),(\d{1,2}),(\d{1,2}),(-?\d{1,2}),(-?\d{1,2}),(-?\d{1,2})(\]\})

Each of the sections in parentheses is a captured group which can later be referred to as $1,$2 etc. There are eight capture groups in all. Naturally, I store this behemoth of an expression in a local variable named ‘x’ so I can use it more easily in further if() functions later in the same formula.

With the prospect of multiple patterns needed for a number of purposes I would like to go one step further and store (tidy) these large patterns in a global variable out of the way but ready to be called as gv(pat) and maybe even in a json array of their own so I could call them with tc(json, gv(pat), x[1]) etc.

The trouble is that if I store the long regex expression externally in a global variable, then it is not returned verbatim. Rather, the string is ‘processed’ and none of the special escaped character definitions are preserved.

Indeed, calling the global variable gv(pat) with the above expression inside as text only returns as below:-

(s:)({1,2}),({1,2}),({1,2}),(-?{1,2}),(-?{1,2}),(-?{1,2})()

which is no good. All the important escaped characters that define the regular expression have been lost. This happens even if I enclose the entire string in quotes.

The solution is comical but logical. The regular expression contains forward slashes which regex uses to escape certain letters such as \d for example to mean digit.

The forward slashes themselves are special characters, so any text global variable containing them (like a big long regular expression) also need to be escaped.

So to store the above regular expression in a separate global variable and for it to be preserved one must escape the escape characters!

(\\{s:\\[)(\\d{1,2}),(\\d{1,2}),(\\d{1,2}),(-?\\d{1,2}),(-?\\d{1,2}),(-?\\d{1,2})(\\]\\})

Such larks.

And if that wasn’t hilarity manifest, I had to edit the post several times before the escaped forward slashes in the end formula weren’t being escaped in the post! Where you see two forward slashes as intended, I had to type three in order that the escaped characters being escaped where not being escaped again. I had to do four in some places.

I guess I should have learnt to use Preformatted text