Expression Language Regex String Substitution

I’m trying to make some standardized tag names look “pretty” on the HMI in perspective and wanting to take tags such as PIT1000A or FALL4305 display as PIT-1000A or FALL-4305 by inserting a hyphen between the “type” of device and the number of the device. I’m trying to do this through the binding when passing in the tag name to just use an expression to do it.

Since there’s no standard length or specific characters of the alphabetic portion and the numeric portion I figured it would best be done with a regular expression to create some capture groups, then use them in a substitution fashion to build out the final name.

Something like my regex would be “^(\D*)(.*)” and the replacement would be “$1-$2” but I’m not finding a way to do this and I figured this would be something already implemented and I’m just overlooking it.

Huh. That should be an easy one to create. I’ll add it to my Simulation Aids module.

You can get pretty close with Ignition, although I’ll be the first to admit the regex is a bit unwieldy, and this is coming from a fan of regex :slight_smile:
split({Root Container.Text Field.text}, "(?<=\\D)(?=\\d)")
groupConcat({Root}, 0, "-")

Someone else might be able to fix it to only match the first occurrence to exactly match the desired output.
I guess it’s close enough if you just split on the first alpha → numeric transition.


What about a script transform? In a project script (I put mine in util):

def insertDash(stringIn):
	import re
	findRegex = r'([0-9].*)'
	replaceRegex = r'-\1'
	return re.sub(findRegex, replaceRegex, stringIn, 0)

Transform script:

return util.insertDash(value)

I’ve got an emergency trip I have to make, but as soon as I get back to normal I’ll give this a shot. Looks like there’s a way so I’m not going crazy.

I ended up taking the script @JordanCClark put and modifying it to go in my Gateway scripts project then using it in my UDT as an expression tag with the runScript expression to get what I call the “DisplayName”

def insertDash(stringIn):
	import re
	findRegex = r'^(\D*)(.*)'
	replaceRegex = r'\1-\2'
	return re.sub(findRegex, replaceRegex, stringIn, 0)

This is what my expression looks like in my UDT:

runScript("util.insertDash", 0, {InstanceName})