regex in change and switch

5,090 views
Skip to first unread message

Brad Hopper

unread,
Jul 28, 2016, 10:52:04 PM7/28/16
to Node-RED
I can't get either of these solutions to do the obvious thing.

I've searched and read that the switch node regex has some limitations, but don't know if I'm hitting those with this simple task.

Trying to use the regex in the Switch node but basically can't get it to match anything. Also it's a bit unclear what the second dropdown should be - I assume I should choose "string" and put the regex in there, but clearly that's not working, even for just a raw string that should match. None of the other dropdown options seem useful for regex.

Then in the Change node the regex does work but the default is "replace" instead of the more obvious "extract", which I think it should be. So now I have to use crazy regex to replace everything but what I want to extract. This seems possible but I've not been able to get the regex to work.

The payload is from Adafruit 1Wire thermocouple board. The msg.payload is 2 lines, with the end of the second line containing a 5 numeral temperature measurement.

I have to use something like this: ^(?![0-9]{5}$).* which will match the entire line (eliminating the other line because it does not contain a 5 numeral string. Can't figure out how to eliminate the rest of the line.

Any thoughts?


Mark Setrem

unread,
Jul 29, 2016, 2:59:01 AM7/29/16
to Node-RED
It might be useful if you could post the msg.payload

Brad Hopper

unread,
Jul 29, 2016, 5:20:24 PM7/29/16
to Node-RED
Thanks Mark, the payload looks like this:

68 01 f0 16 f0 ff ff ff 73 : crc=73 YES
68 01 f0 16 f0 ff ff ff 73 t=22500

By the way I ended up using a function node and using a .match call with the trivial [0-9]{5} regex and it works fine, but still would like to know what I was doing wrong with either Switch or Change regex. I would suggest there should be an "extract" option in the Change node.

Nicholas O'Leary

unread,
Jul 29, 2016, 5:29:01 PM7/29/16
to Node-RED Mailing List
The following works:
   Search for: ^(.|\n)*([0-9]{5})$
   Replace with: $2

But you've made a good point - need to look at how to make that easier.

Nick

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.

Bart Butenaers

unread,
Sep 26, 2017, 4:00:28 PM9/26/17
to Node-RED
Hi Nick,

This is a rather old post, so I don't know if your suggestion above is still valid.
Could you please explain where (in the change node) the $2 can be specified, to get the second match of the regex ?
E.g. the following doesn't work, because it simply stores the string '$2' in the msg.payload (instead of the second match):


Hoped to find something like the following option, but I didn't ;-)

Thanks in advance !!

Bart

Nick O'Leary

unread,
Sep 26, 2017, 4:07:23 PM9/26/17
to Node-RED Mailing List
This works for me:


Inject 'abcd', get out 'cd'

Are you sure your RegEx actually matches the received value?

If you have an example of the data you're matching and the regex you're using (I can't face trying to transpose it from the screenshot you shared...) would be easier to check what's going on.

Nick

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Bart Butenaers

unread,
Sep 26, 2017, 5:20:11 PM9/26/17
to Node-RED
Hi Nick,

Seems indeed working fine!
I assumed incorrectly that the first match $1 would correspond to the entire string, similar to group(0) in Java.  
But indeed first match is $1, second match is $2, ...   
And when I specify an unexisting match $3, the output is just a string '$3'.

Thanks a lot!
I will use this nice feature in Node-Red a lot, starting from now...

Bart

Julian Knight

unread,
Sep 28, 2017, 5:03:44 AM9/28/17
to Node-RED
Isn't $0 the whole string? Not tried it so not sure.

Bart Butenaers

unread,
Sep 28, 2017, 5:42:37 PM9/28/17
to Node-RED
Hi Julian,

Indeed you find all over the internet that $0 matches the entire string.  
However that doesn't work in my Node-Red flow below.  Very weird ...

But following this link it seems you need to use $& instead, and indeed that seems to work fine:


[{"id":"85d9312e.0b00f","type":"inject","z":"72bb69bc.630318","name":"Inject abcd","topic":"","payload":"abcd","payloadType":"str","repeat":"","crontab":"","once":false,"x":200,"y":320,"wires":[["25a2c3f1.74d60c","89ecffdd.12488","190edca8.9844e3","9f09c288.a1f55","2c5eee68.d7d852"]]},{"id":"190edca8.9844e3","type":"change","z":"72bb69bc.630318","name":"$1","rules":[{"t":"change","p":"payload","pt":"msg","from":"^(..)(..)$","fromt":"re","to":"$1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":410,"y":320,"wires":[["f352d66c.81fdf8"]]},{"id":"f352d66c.81fdf8","type":"debug","z":"72bb69bc.630318","name":"$1","active":true,"console":"false","complete":"payload","x":610,"y":320,"wires":[]},{"id":"9f09c288.a1f55","type":"change","z":"72bb69bc.630318","name":"$2","rules":[{"t":"change","p":"payload","pt":"msg","from":"^(..)(..)$","fromt":"re","to":"$2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":410,"y":360,"wires":[["7dab6d3c.9db6c4"]]},{"id":"7dab6d3c.9db6c4","type":"debug","z":"72bb69bc.630318","name":"$2","active":true,"console":"false","complete":"payload","x":610,"y":360,"wires":[]},{"id":"2c5eee68.d7d852","type":"change","z":"72bb69bc.630318","name":"$3","rules":[{"t":"change","p":"payload","pt":"msg","from":"^(..)(..)$","fromt":"re","to":"$3","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":410,"y":400,"wires":[["be497aa5.239558"]]},{"id":"be497aa5.239558","type":"debug","z":"72bb69bc.630318","name":"$3","active":true,"console":"false","complete":"payload","x":610,"y":400,"wires":[]},{"id":"89ecffdd.12488","type":"change","z":"72bb69bc.630318","name":"$0","rules":[{"t":"change","p":"payload","pt":"msg","from":"^(..)(..)$","fromt":"re","to":"$0","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":410,"y":280,"wires":[["8b741259.b17bf"]]},{"id":"8b741259.b17bf","type":"debug","z":"72bb69bc.630318","name":"$0","active":true,"console":"false","complete":"payload","x":610,"y":280,"wires":[]},{"id":"25a2c3f1.74d60c","type":"change","z":"72bb69bc.630318","name":"$&","rules":[{"t":"change","p":"payload","pt":"msg","from":"^(..)(..)$","fromt":"re","to":"$&","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":410,"y":240,"wires":[["19430b6f.958f35"]]},{"id":"19430b6f.958f35","type":"debug","z":"72bb69bc.630318","name":"$&","active":true,"console":"false","complete":"payload","x":610,"y":240,"wires":[]}]


Kind regards,
Bart

Julian Knight

unread,
Sep 29, 2017, 5:01:45 AM9/29/17
to Node-RED
Good find Bart. I've never needed to do that with regex's in Node-RED, it is an annoyance of regex that it isn't quite a single standard. Depends on the regex library being used.

It would be useful to put the information from your post either into the WIKI for the cookbook or as a flow on the flows site so that others can most easily reference it in the future.

Nick O'Leary

unread,
Sep 29, 2017, 5:06:11 AM9/29/17
to Node-RED Mailing List
This is *not* Node-RED specific.

This is *not* about choice of RegEx library.

This is absolutely 'standard' JavaScript.

Once we find the time, or someone chooses to help, the cookbook will cover more content of the core nodes. The cookbook wiki is meant to be a staging post, not the final destination. Someone still needs to actually create the recipe and open a pull-request to get it included. I wish I had more time to get it done - I don't.


Nick


--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Julian Knight

unread,
Sep 29, 2017, 10:08:40 AM9/29/17
to Node-RED


On Friday, 29 September 2017 10:06:11 UTC+1, Nick O'Leary wrote:
This is *not* Node-RED specific.

This is *not* about choice of RegEx library.

JK> Well it really is - but I didn't mean a JavaScript library, I meant something more general e.g the difference between PERL, PHP & JavaScript implementations of regex - my bad for not explaining properly. 

This is absolutely 'standard' JavaScript.

Once we find the time, or someone chooses to help, the cookbook will cover more content of the core nodes. The cookbook wiki is meant to be a staging post, not the final destination. Someone still needs to actually create the recipe and open a pull-request to get it included. I wish I had more time to get it done - I don't.


JK> I get this. I think this was discussed a while ago on Slack - that there really needs to be a slicker process of reviewing WIKI entries and then maybe assigning volunteers to turn the ideas into actual articles. Also mentioned was that some of the entries in the WIKI aren't really so suitable for a cookbook but maybe should be in the docs. Maybe some delegation required? Not suggesting me as my style of writing does tend to end up rather dry and technical and I suspect this role would benefit from slightly different skills :-} However, I am certainly happy to contribute what I can. The key is probably some good written guidelines and a review process.

Nick O'Leary

unread,
Sep 29, 2017, 11:33:48 AM9/29/17
to Node-RED Mailing List
Maybe some delegation required? 

Absolutely. To whom should we delegate?

Without people volunteering to do the work it inevitably falls back to me.





--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Julian Knight

unread,
Sep 29, 2017, 7:21:18 PM9/29/17
to Node-RED
Yes, good point. Maybe start with a call to action? I know that Mike Blackstock has been active. I think the point I'm trying to make is that we need to first agree some standards and processes. Clearly that needs your input. If we (and I'm happy to help) can document those, then we can draft in other people to start adding to documentation more consistently.

This is not a criticism at all by the way, I'm just trying to think of ways we can take some load off you without you losing the overall control you need.


On Friday, 29 September 2017 16:33:48 UTC+1, Nick O'Leary wrote:
Maybe some delegation required? 

Absolutely. To whom should we delegate?

Without people volunteering to do the work it inevitably falls back to me.




On 29 September 2017 at 15:08, Julian Knight <j.kni...@gmail.com> wrote:


On Friday, 29 September 2017 10:06:11 UTC+1, Nick O'Leary wrote:
This is *not* Node-RED specific.

This is *not* about choice of RegEx library.

JK> Well it really is - but I didn't mean a JavaScript library, I meant something more general e.g the difference between PERL, PHP & JavaScript implementations of regex - my bad for not explaining properly. 

This is absolutely 'standard' JavaScript.

Once we find the time, or someone chooses to help, the cookbook will cover more content of the core nodes. The cookbook wiki is meant to be a staging post, not the final destination. Someone still needs to actually create the recipe and open a pull-request to get it included. I wish I had more time to get it done - I don't.


JK> I get this. I think this was discussed a while ago on Slack - that there really needs to be a slicker process of reviewing WIKI entries and then maybe assigning volunteers to turn the ideas into actual articles. Also mentioned was that some of the entries in the WIKI aren't really so suitable for a cookbook but maybe should be in the docs. Maybe some delegation required? Not suggesting me as my style of writing does tend to end up rather dry and technical and I suspect this role would benefit from slightly different skills :-} However, I am certainly happy to contribute what I can. The key is probably some good written guidelines and a review process.

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
Reply all
Reply to author
Forward
0 new messages