Quantcast
Channel: User mikyll98 - Stack Overflow
Viewing all articles
Browse latest Browse all 102

Answer by mikyll98 for How to check if a regex string is PCRE-compliant?

$
0
0

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 *)

Viewing all articles
Browse latest Browse all 102

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>