# Trying to get substring out of string

**URL:** <https://forum.inductiveautomation.com/t/trying-to-get-substring-out-of-string/87355>\
**Category:** Ignition\
**Created:** [March 14, 2024, 4:48pm UTC](https://forum.inductiveautomation.com/t/trying-to-get-substring-out-of-string/87355 "2024-03-14T16:48:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryan\_Craig](https://avatars.discourse-cdn.com/v4/letter/r/45deac/32.png) [@Ryan\_Craig](https://forum.inductiveautomation.com/u/Ryan_Craig)\
**Post date:** [March 14, 2024, 4:48pm UTC](https://forum.inductiveautomation.com/t/trying-to-get-substring-out-of-string/87355/1 "2024-03-14T16:48:40Z")

</div>

I am making a project where someone will scan a data matrix that will fill out a text box and displays a string like this:  
"colderAQGdataPN: AQG17006HTBN:85065002023/06/22"

I am trying to get 2 sub strings from this string but I am struggling to do so.

I am trying to get the string that is in between 'PN: ' and 'BN:' (should be: AQG17006HT) and the string between 'BN:' and '2023' which is a date that will be changing (should be: 8506500) .

So far I have created a custom property on the text box and have tried an expression binding that looks like this:

split{this.props.text}, 'PN: ')

But I am having no luck.

Any ideas?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [March 14, 2024, 4:54pm UTC](https://forum.inductiveautomation.com/t/trying-to-get-substring-out-of-string/87355/2 "2024-03-14T16:54:52Z")

</div>

You could write a script module function that uses regular expression to match the 2 components:

```auto
>>> data = "colderAQGdataPN: AQG17006HTBN:85065002023/06/22"
>>> import re
>>> m = re.search('.*PN: (.+)BN:([0-9]{7}).*', data)
>>> m.group(0)
'colderAQGdataPN: AQG17006HTBN:85065002023/06/22'
>>> m.group(1)
'AQG17006HT'
>>> m.group(2)
'8506500'

```

---

<div class="post-metadata">

**Author:** ![Ryan\_Craig](https://avatars.discourse-cdn.com/v4/letter/r/45deac/32.png) [@Ryan\_Craig](https://forum.inductiveautomation.com/u/Ryan_Craig)\
**Post date:** [March 14, 2024, 5:25pm UTC](https://forum.inductiveautomation.com/t/trying-to-get-substring-out-of-string/87355/3 "2024-03-14T17:25:28Z")

</div>

This seems to work when I try it in the script console but I can't figure out where to put it in the project. Do I put it on a tags value change event, or a custom property?

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/7/67afbebabe311bee68d765b9f0dbeee83733e06e.png)

This is what I have so far.

EDIT: I am able to get it to work with  
data = "colderAQGdataPN: AQG17006HTBN:85065002023/06/22"  
But I am hoping to make that string a variable based on the value of a textbox

---

<div class="post-metadata">

**Author:** ![Ryan\_Craig](https://avatars.discourse-cdn.com/v4/letter/r/45deac/32.png) [@Ryan\_Craig](https://forum.inductiveautomation.com/u/Ryan_Craig)\
**Post date:** [March 27, 2024, 3:21pm UTC](https://forum.inductiveautomation.com/t/trying-to-get-substring-out-of-string/87355/4 "2024-03-27T15:21:25Z")

</div>

This code seems to work for this:

"colderAQGdataPN: AQG17006HTBN:85065002023/06/22"

but when I scan a different part it doesn't seem to work:

[colder.com/AQGdataPN:](http://colder.com/AQGdataPN:) AQG17006HTBN: 85065002023/06/22

This new string seems to be the more common string too.

Any ideas how to make it work with the new string?

---

<div class="post-metadata">

**Author:** ![Felipe\_CRM](https://avatars.discourse-cdn.com/v4/letter/f/dfb087/32.png) [@Felipe\_CRM](https://forum.inductiveautomation.com/u/Felipe_CRM)\
**Post date:** [March 27, 2024, 3:34pm UTC](https://forum.inductiveautomation.com/t/trying-to-get-substring-out-of-string/87355/5 "2024-03-27T15:34:49Z")

</div>

The spaces break the regular expression, so you'll need to play with it to optionally ignore that space:

(I recommend [https://regex101.com/](https://regex101.com/) or a similar website that gives you live feedback and explanation of the regex)

Anyway, `'.*PN: *(.+)BN: *([0-9]{7}).*'` will work whether there are additional spaces after `PN.` or `BN.` or not.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [March 27, 2024, 3:35pm UTC](https://forum.inductiveautomation.com/t/trying-to-get-substring-out-of-string/87355/6 "2024-03-27T15:35:01Z")

</div>

This may work: `.*PN: (.+)BN:\\s*([0-9]{7}).*`

It adds allowance for 0 or more whitespace characters before the 2nd group.

---

<div class="post-metadata">

**Author:** ![Ryan\_Craig](https://avatars.discourse-cdn.com/v4/letter/r/45deac/32.png) [@Ryan\_Craig](https://forum.inductiveautomation.com/u/Ryan_Craig)\
**Post date:** [March 27, 2024, 4:21pm UTC](https://forum.inductiveautomation.com/t/trying-to-get-substring-out-of-string/87355/7 "2024-03-27T16:21:41Z")

</div>

This seemed to work perfectly! I will do some testing just to make sure there aren't any weird strings. Thanks!
