Duct Tape a Regex Decider
Perl 6, 20 snippets



{$_ eq m/[[&lt;-[()*?|\\]&gt;|\\&lt;[()*?|\\]&gt;|'(' &lt;~~&gt;* ')']&lt;[*?]&gt;?|\|]+/}


Try it online!

The snippets are taken from:

{$_ eq, m/[, &lt;-[, ()*?, |\\, ]&gt;, |\\, &lt;[, ()*?, |\\, ]&gt;, |, '(' &lt;~~&gt;* ')', &lt;[, *?, ]&gt;, ?|, \|, ]+/, }.

This is mostly the greedy approach (made obvious by all the one or two character snippets). I used SymbolHound to search for the individual characters, and the only real optimisation was the '(' &lt;~~&gt;* ')' snippet, which is taken from my own answer on recursive Perl 6 regexes.

Explanation:

This basically checks if the input is equal to a greedy match of a valid regex. The reason we can't just use the regex itself and add ^$ to mark the ends is because we are using a recursive regex, which wouldn't work if there were ^$ markers. The regex itself is:

m/[                             ]+/   # Match one or more times
   [              ]  # Any of 
    &lt;-[()*?|\\]&gt; |     # Not a metacharacter
    \\&lt;[()*?|\\]&gt;      # A metacharacter preceded by a \
    '(' &lt;~~&gt;* ')'      # Brackets surrounding a valid regex
                   &lt;[*?]&gt;?  # Optionally followed by a ? or *
                           | \|    # Or just the | metacharacter