Thanks to @InSync for the suggestion :) this gave me an insight on a possible solution.
One can simply use a regex engine and pcall (~try-catch) the pattern creation block.
Example using lrexlib-pcre
library:
local rex = require("rex_pcre")local function is_valid_regex(pattern) return pcall(rex.new, pattern)end-- Testprint(is_valid_regex(".*")) -- trueprint(is_valid_regex("[a-z]+")) -- trueprint(is_valid_regex("([0-9]")) -- false (unbalanced parentheses)print(is_valid_regex("*invalid")) -- false (invalid usage of *)