Slicing a String


I´m reading a string tag over OPC connection:

“Job222222Guid”: “863b5079-6c42-4f3f-a865-bda506d7a555”,
“ExternalJobGuid”: “00000000-0000-0000-0000-000000000000”,
"Name": “JOB069216-002”,
“Description”: “#FormatType#X3000Y1200”,
“UserInfo1”: “”,
“UserInfo2”: “”,
“UserInfo3”: “”,
“MeasurementSystem”: 0,
“NcProgramFile”: “S:\JOB069216-002.LCC”

I created a new ExpressionTag to read only the Job Name:


the substring is JOB069216 was expected, i have a problem when the number of caracteres are incremented or decremented because my substring doesnt works right.

I need to do something like Slicing but i dont have this expression inside the tags, anyone already solved this problem ?? Anyone have some idea or example ??

What about using Split?

1 Like

I’ll start off here by saying that using split as @josborn mentions might be the best solution. However, your inquiry got me thinking what it might look like to keep those associated pieces with the original tag. So I thought I’d share this since it was a fun little academic/thought exercise:

Tag Change Script content
def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	import re
	# Use named groups in the regex that match your custom property definitions
	p = re.compile('^(?P<JobName>JOB\d+)-(?P<JobSuffix>.*)$')
	# Attempt a match against the current value
	m = p.match(currentValue.value)
	# If we've got a match...
	if (m is not None):
		# pull named groups out of match and assemble list of tag paths
		tag_paths = ['{0}.{1}'.format(tagPath, key) for key in m.groupdict().keys()]
		# pull associated values from named groups for list of tag values
		tag_values = [val for val in m.groupdict().values()]
		# write to local custom properties
		system.tag.writeBlocking(tag_paths, tag_values)
		# ... otherwise, perform error handling in the event of a non-match

I think I’d probably hoist this functionality out into a more generalized script library function that I could invoke with a much simpler call syntax (i.e. one line) in the tag event script if I were to go down this route on a production app. But like I said at the onset, keeping it simple definitely has a LOT of advantages. :slight_smile:


Since the value is JSON I’d expect you could use the jsonGet expression function before splitting on “-” to remove it. Or maybe I missed something.