# Expression Language Regex String Substitution

**URL:** <https://forum.inductiveautomation.com/t/expression-language-regex-string-substitution/53587>\
**Category:** Ignition\
**Created:** [November 30, 2021, 8:33pm UTC](https://forum.inductiveautomation.com/t/expression-language-regex-string-substitution/53587 "2021-11-30T20:33:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![michael.flagler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/michael.flagler/32/26272_2.png) [@michael.flagler](https://forum.inductiveautomation.com/u/michael.flagler)\
**Post date:** [November 30, 2021, 8:33pm UTC](https://forum.inductiveautomation.com/t/expression-language-regex-string-substitution/53587/1 "2021-11-30T20:33:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [November 30, 2021, 8:52pm UTC](https://forum.inductiveautomation.com/t/expression-language-regex-string-substitution/53587/2 "2021-11-30T20:52:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [November 30, 2021, 9:01pm UTC](https://forum.inductiveautomation.com/t/expression-language-regex-string-substitution/53587/3 "2021-11-30T21:01:06Z")

</div>

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 🙂  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/1/f10960c9eda58cbe9290d295bee16c5e19bcd3ef.png)  
`split({Root Container.Text Field.text}, "(?<=\\D)(?=\\d)")`  
`groupConcat({Root Container.Table.data}, 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.  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/3/239c3d72f975ed927beface706f2c1e35361ad7a.png)

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [November 30, 2021, 9:58pm UTC](https://forum.inductiveautomation.com/t/expression-language-regex-string-substitution/53587/4 "2021-11-30T21:58:46Z")

</div>

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

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

```

Transform script:

```python
return util.insertDash(value)

```

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/5/1/51cd8ab72fdbdd1506f3764825e1bda35c9c866e.png)

---

<div class="post-metadata">

**Author:** ![michael.flagler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/michael.flagler/32/26272_2.png) [@michael.flagler](https://forum.inductiveautomation.com/u/michael.flagler)\
**Post date:** [December 1, 2021, 1:57am UTC](https://forum.inductiveautomation.com/t/expression-language-regex-string-substitution/53587/5 "2021-12-01T01:57:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![michael.flagler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/michael.flagler/32/26272_2.png) [@michael.flagler](https://forum.inductiveautomation.com/u/michael.flagler)\
**Post date:** [December 2, 2021, 4:30am UTC](https://forum.inductiveautomation.com/t/expression-language-regex-string-substitution/53587/6 "2021-12-02T04:30:22Z")

</div>

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”

```auto
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:

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

```
