Search code examples
lualua-patterns

Lua pattern in mqtt topics


Here is a standard topic pattern which is used in mqtt.

"lights/hue/{device_name}/get/sensing"

How could I use the regular expression to format this topic pattern with a real device name.

I am not very into the regular expression, so what I would need is a function to make a topic given a device name.

for example,

pattern : "lights/hue/{device_name}/get/sensing"
input : name = 'device123'
output: "lights/hue/device123/get/sensing"

Currently I am using the lua, would someone help me?


Solution

  • Assuming Lua:

    pattern = "lights/hue/{device_name}/get/sensing"
    name = "device123"
    output = string.gsub(pattern, "{device_name}", name )
    print(output)
    

    There is no need for a regular expression. Simple replacement will do the job.