Regex validating regex Build a regex that will accept a regex string as input and check if it's valid. Basically, your regex should be able to validate itself. (Any invalid regex should not be validated, so you can't use .*. ;))

Your flavor must be fully supported by well known implementations (Perl, sed, grep, gawk, etc), and it must fully support what those implementations support. [Don't worry about the lawyer speak; I'm just trying to remove any possible loopholes for the smart***es.]



I'd code-golf it, but I'm worried it'll give an edge to those who know and use non-feature-rich flavors. Or are my worries unfounded?
Ruby

I tried to match the actual syntax of Ruby's regex flavor as much as possible, but there are a few quirks: it accepts a few lookbehinds that are actually invalid (like (?&lt;=(?&lt;!))), and it recognizes empty character ranges like D-A. The latter could be fixed for ASCII, but the regex is long enough as it is.

\A(?&lt;main&gt;
    (?!
        \{(\d+)?,(\d+)?\} # do not match lone counted repetition
    )
    (?:
        [^()\[\]\\*+?|&lt;'] | # anything but metacharacters
        (?&lt;cclass&gt;
            \[ \^? (?: # character class
                (?: # character class
                    [^\[\]\\-] | # anything but square brackets,  backslashes or dashes
                    \g&lt;esc&gt; |
                    \[ : \^? (?: # POSIX char-class
                        alnum | alpha | word | blank | cntrl | x?digit | graph | lower | print | punct | space | upper
                    ) : \] |
                    - (?!
                        \\[dwhsDWHS]
                    ) # range / dash not succeeded by a character class
                )+ |
                \g&lt;cclass&gt; # more than one bracket as delimiter
            ) \]
        ) |
        (?&lt;esc&gt;
            \\[^cuxkg] | # any escaped character
            \\x \h\h? | # hex escape
            \\u \h{4} | # Unicode escape
            \\c . # control escape
        ) |
        \\[kg] (?:
            &lt; \w[^&gt;]* (?: &gt; | \Z) |
            ' \w[^']* (?: ' | \Z)
        )? | # named backrefs
        (?&lt;! (?&lt;! \\) \\[kg]) [&lt;'] | # don't match &lt; or ' if preceded by \k or \g
        \| (?! \g&lt;rep&gt; ) | # alternation
        \( (?: # group
            (?:
                \?
                (?:
                    [&gt;:=!] | # atomic / non-capturing / lookahead
                    (?&lt;namedg&gt;
                        &lt; [_a-zA-Z][^&gt;]* &gt; |
                        ' [_a-zA-Z][^']* ' # named group
                    ) |
                    [xmi-]+: # regex options
                )
            )?
            \g&lt;main&gt;*
        ) \) |
        \(\?&lt;[!=] (?&lt;lbpat&gt;
            (?! \{(\d+)?,(\d+)?\} )
            [^()\[\]\\*+?] |
            \g&lt;esc&gt;  (?&lt;! \\[zZ]) |
            \g&lt;cclass&gt; |
            \( (?: # group
                (?:
                    \?: |
                    \? \g&lt;namedg&gt; |
                    \? &lt;[!=]
                )?
                \g&lt;lbpat&gt;*
            ) \) |
            \(\?\# [^)]* \)
        )* \)
        |
        \(\? [xmi-]+ \) # option group
        (?! \g&lt;rep&gt; ) 
        |
        \(\?\# [^)]*+ \) # comment
        (?! \g&lt;rep&gt; )
    )+
    (?&lt;rep&gt;
        (?:
            [*+?] | # repetition
            \{(\d+)?,(\d+)?\} # counted repetition
        )
        [+?]? # with a possessive/lazy modifier
    )?
)*\Z


Unreadable version:

\A(?&lt;main&gt;(?!\{(\d+)?,(\d+)?\})(?:[^()\[\]\\*+?|&lt;']|(?&lt;cclass&gt;\[\^?(?:(?:[^\[\]\\-]|\g&lt;esc&gt;|\[:\^?(?:alnum|alpha|word|blank|cntrl|x?digit|graph|lower|print|punct|space|upper):\]|-(?!\\[dwhsDWHS]))+|\g&lt;cclass&gt;)\])|(?&lt;esc&gt;\\[^cuxkg]|\\x\h\h?|\\u\h{4}|\\c.)|\\[kg](?:&lt;\w[^&gt;]*(?:&gt;|\Z)|'\w[^']*(?:'|\Z))?|(?&lt;!(?&lt;!\\)\\[kg])[&lt;']|\|(?!\g&lt;rep&gt;)|\((?:(?:\?(?:[&gt;:=!]|(?&lt;namedg&gt;&lt;[_a-zA-Z][^&gt;]*&gt;|'[_a-zA-Z][^']*')|[xmi-]+:))?\g&lt;main&gt;*)\)|\(\?&lt;[!=](?&lt;lbpat&gt;(?!\{(\d+)?,(\d+)?\})[^()\[\]\\*+?]|\g&lt;esc&gt;(?&lt;!\\[zZ])|\g&lt;cclass&gt;|\((?:(?:\?:|\?\g&lt;namedg&gt;|\?&lt;[!=])?\g&lt;lbpat&gt;*)\)|\(\?#[^)]*\))*\)|\(\?[xmi-]+\)(?!\g&lt;rep&gt;)|\(\?#[^)]*+\)(?!\g&lt;rep&gt;))+(?&lt;rep&gt;(?:[*+?]|\{(\d+)?,(\d+)?\})[+?]?)?)*\Z