# Switch node - wildcard option for routing

**URL:** <https://discourse.nodered.org/t/switch-node-wildcard-option-for-routing/46454>\
**Category:** Feature Requests\
**Created:** [29 May 2021 17:55 UTC](https://discourse.nodered.org/t/switch-node-wildcard-option-for-routing/46454 "2021-05-29T17:55:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [29 May 2021 17:55 UTC](https://discourse.nodered.org/t/switch-node-wildcard-option-for-routing/46454/1 "2021-05-29T17:55:39Z")

</div>

Hi all, following on from the thread [Switch node - MQTT like wildcards for switching](https://discourse.nodered.org/t/switch-node-mqtt-like-wildcards-for-switching/46337/78) that was raised following some interesting points in [this thread](https://discourse.nodered.org/t/message-structure-convention/46324), it is proposed that it would be ultimately more beneficial to everyone to have a wildcard option for switch routing.

This wildcard option would be more flexible than MQTT filter while being simpler than regex and JSONata and so being more accessible.

Several of the folk who commented on the previous thread alluded to the familiar format of using `*, ?, #`. This is a pretty common syntax as described [here on wikipedia](https://en.wikipedia.org/wiki/Glob_(programming)) and [here in MS docs](https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/like-operator)

Examples...

| Wildcard | Description | Example | Matches | Does not match |
| --- | --- | --- | --- | --- |
| `*` | matches any number of any characters including none | `*Law*` | `Law` , `GrokLaw` , or `Lawyer` . | `La` , or `aw` |
| `#` | matches any single number | `no#` | `no1` , `no2` ~ `no9` | `nod` or `o2` or `no` |
| `?` | matches any single character | `?at` | `Cat` , `cat` , `Bat` or `bat` | `at` |
| `[abc]` | matches one character given in the bracket | `[CB]at` | `Cat` or `Bat` | `cat` , `bat` or `CBat` |
| `[!abc]` | Dont match one character given in the bracket | `[!CB]at` | `Rat` or `Hat` | `Cat` , `Bat` or `CBat` |
| `[a-z]` | matches one character from the (locale-dependent) range given in the bracket | `Letter[0-9]` | `Letter0` , `Letter1` , `Letter2` up to `Letter9` | `Letters` , `Letter` or `Letter10` |

  

With regards to MQTT topic matching, this would now be expressed as...

| MQTT filter | glob style |
| --- | --- |
| `#` | `?*` |
| `home/#` | `home/?*` |
| `home/+/#` | `home/?*/?*` |
| `home/+/temperature` | `home/?*/temperature` |
| `home/+/+/sensors/#` | `home/?*/?*/sensors/?*` |
| `home/+` | not possible (`*1`) |
| `home/+/+` | not possible (`*2`) |

\*1 : `home/+` not possible - closest possible filter is `home/?*` but that will not stop at level 2  
\*2 : `home/+/+` not possible - closest possible filter is `home/?*/?*` but that will not stop at level 3

  

### Discussion points...

- Add `+` as a wildcard with the definition of **"1 or more characters up to a `/`"** to handle points (\*1) & (\*2) ?
- Support for Digraph Characters (e.g. `æ` would equal `a` and `e`)?
- Support for Collating Sequence (e.g. `[A–E]` matches `A` ,`À` , `B` , `C` , `D` and `E`. The range does not match `Ê` because accented characters collate after unaccented characters in the sort order)?

  

### Personal thoughts...

_Firstly, apologies up front, I mean no offence to anyone, these are personal thoughts and musings._  
I might be wrong (it happens) but as I write this up, I see enough limitations and complexity in implementation that makes me think this wildcard proposal is a case of more effort than benefit considering regex does it all + more. And please don't bite my head off here but I think this tells a tale - there is _possibly_ some mileage in the originally proposed MQTT filter (suitably simple for its purpose at 20 lines of code!). I no longer know TBH :sigh: (sorry folks)

  

That said, lets see where this goes - perhaps I am to close to this - fresh eyes never hurt 🙂

  

**Ideas / thoughts / comments below please**

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [29 May 2021 18:54 UTC](https://discourse.nodered.org/t/switch-node-wildcard-option-for-routing/46454/2 "2021-05-29T18:54:52Z")

</div>

> [@Steve-Mcl](#):
>
> ....makes me think this wildcard proposal is a case of more effort than benefit considering regex does it all + more.

I tend to agree.  
Plan A seemed like a good idea which would benefit inexperienced users, but this does not move us on much further from what we have at the moment.  
Also, could adding `+` as a wildcard confuse the regex geeks, as it's not in the official regex schema.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [29 May 2021 18:55 UTC](https://discourse.nodered.org/t/switch-node-wildcard-option-for-routing/46454/3 "2021-05-29T18:55:25Z")

</div>

Frankly, I think that anything more than the simplest \* = any number of characters and ? = 1 character is too much. You can use regex for that. I'd personally be happy with the above or with the MQTT style, it is good enough for what I'd use it for and it would help avoid some complex JavaScript nodes.

Keep it simple.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [29 May 2021 18:59 UTC](https://discourse.nodered.org/t/switch-node-wildcard-option-for-routing/46454/4 "2021-05-29T18:59:47Z")

</div>

+1 for KISS

---

<div class="post-metadata">

**Author:** ![Hypnos](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hypnos/32/7566_2.png) [@Hypnos](https://discourse.nodered.org/u/Hypnos)\
**Post date:** [29 May 2021 19:14 UTC](https://discourse.nodered.org/t/switch-node-wildcard-option-for-routing/46454/5 "2021-05-29T19:14:15Z")

</div>

Like others here, I advocate not over-designing this. According to the KISS principle only \* for any and ? for one character. Everything that goes beyond that can be solved with regex.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [5 June 2021 15:22 UTC](https://discourse.nodered.org/t/switch-node-wildcard-option-for-routing/46454/6 "2021-06-05T15:22:22Z")

</div>

Not sure if my beginner's point of view is worth much but...

The Switch node's list of value rules is already so long that you have to use the scrollbar to see them and some of their functions already overlap.  
Please don't add yet another option "wildcard".

On the surface the "contains" option looks nice and simple.  
I feel the "contains" value should be treated as far as possible as a literal with no special character meanings.  
A value of "Chanel #5" should match 'Chanel #5' and not 'Chanel 75'.  
If I want to match either Chanel #5 or Chanel 75 I know I can do it with a simple regex.

The fact that regexes can be difficult to construct and understand is not a good reason to provide in addition a simplified subset under a different name and perhaps slightly different syntax.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [5 June 2021 15:31 UTC](https://discourse.nodered.org/t/switch-node-wildcard-option-for-routing/46454/7 "2021-06-05T15:31:35Z")

</div>

@jbudd I agree (almost) entirely with what you are saying & why I have pretty much dropped this now.

The original request was for a MQTT-like topic filter which is not so easy in regex nor JSONata. Example...

> [@Switch node - MQTT like wildcards for switching](https://discourse.nodered.org/t/switch-node-mqtt-like-wildcards-for-switching/46337/58):
>
> e.g. to achieve a really simple topic filter of `+/1/+/temperature` in regex is `^[^\/\n]+\/1\/[^\/\n]+\/temperature$` 🤮
> 
> [example](https://regex101.com/r/bHRkFL/1)

But ultimately it was rejected so I made a [subflow](https://flows.nodered.org/flow/e38554543a8cdec6c44eacfc68a0c149) instead.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [4 August 2021 15:31 UTC](https://discourse.nodered.org/t/switch-node-wildcard-option-for-routing/46454/8 "2021-08-04T15:31:59Z")

</div>

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