【问题标题】:Lua pattern in mqtt topicsmqtt 主题中的 Lua 模式
【发布时间】:2016-11-23 10:45:03
【问题描述】:

这是 mqtt 中使用的标准主题模式。

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

我如何使用正则表达式将这个主题模式格式化为一个真实的设备名称。

我不是很喜欢正则表达式,所以我需要一个函数来为给定设备名称的主题创建一个函数。

例如,

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

目前我正在使用 lua,有人可以帮助我吗?

【问题讨论】:

  • 请展示您尝试过的内容并限制为一种语言。由于您没有使用 Swift/Objective-C,因此可以删除 nsregularexpression 标签。
  • 你想要的输入和输出是什么?
  • 示例如上。
  • var output = s.replace("{device_name}", name) - 你真的没有试过吗?

标签: lua lua-patterns


【解决方案1】:

假设 Lua:

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

不需要正则表达式。简单的替换就可以了。

【讨论】:

    猜你喜欢
    • 2016-06-10
    • 1970-01-01
    • 1970-01-01
    • 2020-03-23
    • 2013-10-29
    • 1970-01-01
    • 2019-01-23
    • 1970-01-01
    • 2018-08-14
    相关资源
    最近更新 更多