Can I invoke a macro inside another?


use regex_static::lazy_regex;

static PATTERN: Lazy<Regex> = lazy_regex!(
    concat!("^(COMMAND1 (?P<cmd1_args>))|",
            "(COMMAND2 (?P<cmd2_args>))$")

(The above does not work, is is possible to make it work?)

Essentially, the macro takes the text between its parentheses as an argument and can do whatever it likes with it. So, the answer to your question depends on the implementation of the outer macro— If it copies its arguments into the code it generates, then inner macros will probably work. If, on the other hand, it’s trying to choose between different options based on its argument, then inner macros will probably not work.

I’m not familiar with lazy_regex, but I suspect it’s more like the latter case. (Especially given the error you describe). If I had to guess, it probably requires an explicit string literal only, rather than an arbitrary expression.

1 Like

regex_static expects something that can be understood by regex_syntax::Parser. (see here)

Personnally I would use directly once_cell::sync::Lazy and regex::Regex for a regular expression I know to be correct. I don't particularly need it to be checked at each compilation.