lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Why try to convert the matching string into a regexp or Lua pattern at all? Sometimes regexp-type code is simply the wrong way to do things, but something we do merely from familiarity.

It sounds as though matching an input string X against the expression “/sport/#” in the fashion you describe is equivalent to:

X == “/sport” or X:sub(1,7) == “/sport/“

So why not just do it like that, and sidestep regexps, patterns, wildcards, state machines, and all that complexity? The code above is simpler, faster and self-documenting…

(You can make the code generic, of course, with the pattern being a parameter, but you get the idea.)

> On 14 Dec 2022, at 20:54, Tristan Kohl <> wrote:
> Hi folks,
> I am implementing a module that mimics a MQTT broker. For this I am
> transforming topics passed to the register function so I can run
> string.match() on incoming messages.
> Right now I am stuck how to correctly handle the multilevel wildcard:
> Suppose the topic filter "/sport/#" and these published message topics:
> /sport
> /sport/racing
> /sport/racing/champion
> /sporting
> Only the first three are allowed to match, so "/sport.*" is not an
> option as that would match the forth one as well. On the other hand
> "/sport/.*" would not match the first one.
> Any ideas?
> Cheers
> Tristan