Trim text off the beginning and end of a string?

I have an MQTT topic where the payload is this…

,“SSerialReceived”:“S0PTH5VM”}

I am trying to use a rule to take that value and update another string item with…

S0PTH5VM

I have started a rule with a “replace” but I don’t seem to be having much luck…
Any pointers?

rule “Ventillation Fan Serial”

when
   Item VentillationSerial changed
then
   var String ReplaceTest = VentillationSerial.state.replace(',"SSerialReceived":"', '')

   VentillationSerial2.postUpdate(ReplaceTest)
end

Tried a different approach based on findings in the forum and still get an error though…

rule "Ventillation Fan Serial"
when
   Item VentillationSerial changed
then
   var String ReplaceTest = VentillationSerial.state
   ReplaceTest=ReplaceTest.replace(',"SSerialReceived":"','')
   ReplaceTest=ReplaceTest.replace('"}','')

   VentillationSerial2.postUpdate(ReplaceTest)
end

Rule ‘Ventillation Fan Serial’: An error occurred during the script execution: Could not invoke method: java.lang.String.replace(java.lang.CharSequence,java.lang.CharSequence) on instance: ,“SSerialReceived”:“S0PTH5VM”}

Try this…

rule "Ventillation Fan Serial"
when
   Item VentillationSerial changed
then
   val ReplaceTest = triggeringItem.state.toString().replace(',"SSerialReceived":"', '').replace('"}', '')
   VentillationSerial2.postUpdate(ReplaceTest)
end

The topic fragment string you have shown us looks to me like part of a JSON payload.
You might be able to treat it as JSON in the binding or in a rule, rather than getting into direct string manipulation.

1 Like

And if it’s not a fully formed JSON, you can use the REGEX transform.

.*SSerialReceived\"\:\"(.*)\".*

Thanks guys, this is the output from a Serial Bridge using Tasmota. It does resemble JSON but first I’ll just try the trim approach…