Change Node Regex

So I read through a few posts but none of them seemed to be exactly what I was looking for. I know I could do this with a function node but was trying to keep things as "no-code" as possible.

I have a string OKUMA.ASSET.3439.123456 and I'd like to extract the 4 digit number.

I have a Change node with regex set to \.[0-9]+\.. Ultimately I want to return just the number, but baby steps. Right now I just want it to return the match but in the "Replace with" inserting $1 is taken literally.

1 Like

You need to use capture groups ( ) to use $1
eg.

$replace($$.payload, /(\.\d+\.)/, "test$1")

To return just the 4 digit number i would split on "." and select element [2]

I have the following and it doesn't quite seem to work. I tried testing just using "payload" in the JSONata editor and it did work, so is there something I need to do with the path? I copied it from the payload.

Please provide an example flow with the payload in an the inject node. so i can see the data. I can not do much with a screenshot as i have no data to see if your path is correct.
something like below

[{"id":"d17a881.81b99f8","type":"inject","z":"c791cbc0.84f648","name":"The data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"topic\":\"location\",\"payload\":1},{\"topic\":\"location\",\"payload\":\"ddd.fff.3456.33333\"},{\"topic\":\"cockpit\",\"payload\":1},{\"topic\":\"cockpit\",\"payload\":2},{\"topic\":\"battery_status\",\"payload\":1},{\"topic\":\"battery_status\",\"payload\":2}]","payloadType":"json","x":190,"y":840,"wires":[["39f9ee50.cd87aa"]]},{"id":"39f9ee50.cd87aa","type":"change","z":"c791cbc0.84f648","name":"the exprression","rules":[{"t":"set","p":"payload","pt":"msg","to":"$split($$.payload[1].payload, \".\")[2]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":880,"wires":[["c4f45a89.bcb1d8"]]},{"id":"c4f45a89.bcb1d8","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":610,"y":840,"wires":[]}]

To extract the 4-digit number with regex , you can use the following Change Node:

[{"id":"4455bdb3430ed352","type":"change","z":"ce8d30fe991b178a","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":".*\\.(\\d{4})\\..*","fromt":"re","to":"$1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":480,"wires":[["fc4ac1c568fbc98f"]]},{"id":"fc4ac1c568fbc98f","type":"debug","z":"ce8d30fe991b178a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":970,"y":480,"wires":[]},{"id":"354959880a204d2d","type":"inject","z":"ce8d30fe991b178a","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"OKUMA.ASSET.3439.123456","payloadType":"str","x":440,"y":480,"wires":[["4455bdb3430ed352"]]}]

IT is doing security updates on my RHEL server I run this from so I'll have to put that together Monday. Thanks for the help so far!

Thanks @mickym2 that got it!

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.