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?
You could write a script module function that uses regular expression to match the 2 components:
>>> 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'
1 Like
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?
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
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: 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?
The spaces break the regular expression, so you'll need to play with it to optionally ignore that space:
(I recommend 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.
This may work: .*PN: (.+)BN:\\s*([0-9]{7}).*
It adds allowance for 0 or more whitespace characters before the 2nd group.
1 Like
This seemed to work perfectly! I will do some testing just to make sure there aren't any weird strings. Thanks!