Split Regex expression to get tag OPCItemPath 'Device' name

Hi,

I’m trying to write a regex in a split expression to pull the device (PLC) name from the OPCItemPath of a tag, but what I have seems to be pulling the inverse of what I’m expecting and what online resources tell me should work.

I have this so far:

split(toStr(tag("Receivals/Crushers/Crusher 1/AV01.OPCItemPath")), "\\[(.*?)\\]")

If the returned string from the tag read is: ns=1;s=[Crushers_1234]C1_AV01.Sts
I expect the returned value to be: "Crushers_1234"
However, the returned dataset for the above split is this:

[0,0] => "ns=1;s="
[1,0] => "C1_AV01.Sts"

which completely misses the string in square brackets…

This same regex (minus escape characters for the backslashes required by Ignition, e.g. \[(.*?)\] )works in an online regex tester (https://regexr.com) and is also a solution from a Stack Overflow post (https://stackoverflow.com/questions/2403122/regular-expression-to-extract-text-between-square-brackets) where it returns: [Crushers_1234]
which is far closer to what I want… I still don’t know how to remove the brackets though, using the regex language.

Can someone please clue me in on what I’m missing here?

Cheers!

Edit:
It looks like (?<=\[).+?(?=\]) should be the regex expression to remove the brackets, however this is still not returning the text within the brackets, but rather the text either side of the brackets… Again, this works as expected using the online tester.

You are misunderstanding the regex parameter of the split function. From the docs:

The split occurs wherever the regular expression regex occurs.

So with your regex, you tell the function to split the input string at the device name and return the parts before and after the split.
If you want to split at the square brackets, simply use
split("ns=1;s=[Crushers_1234]C1_AV01.Sts","[\\[\\]]")

Yep, that does the trick :sweat_smile: cheers