<story><title>Is there a regular expression to detect a valid regular expression?</title><url>https://stackoverflow.com/questions/172303/is-there-a-regular-expression-to-detect-a-valid-regular-expression</url></story><parent_chain><item><author>hk__2</author><text>It’s a famous post on StackOverflow, but I don’t find it particularly helpful.</text></item><item><author>abraCadabstrax</author><text>Perhaps the most epic reply I have ever seen on SO.</text></item><item><author>gamache</author><text>I believe Zalgo has the answer to this, via an equivalent question. &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;stackoverflow.com&amp;#x2F;questions&amp;#x2F;1732348&amp;#x2F;regex-match-open-tags-except-xhtml-self-contained-tags&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;stackoverflow.com&amp;#x2F;questions&amp;#x2F;1732348&amp;#x2F;regex-match-open...&lt;/a&gt;</text></item></parent_chain><comment><author>kortex</author><text>I found it extremely helpful. The sheer emphasis of the reply made me very curious why the idea of using regex on xml is so bonkers.&lt;p&gt;- I will never forget that regex can&amp;#x27;t parse XHTML&lt;p&gt;- the reason being, regex is insufficiently powerful&lt;p&gt;- when I first saw this post, I knew little about regex under the hood, this sent me down a wiki hole of FSMs, pushdown automata and turing machines&lt;p&gt;- this misconception is apparently common enough to be madness-inducing to those that know better&lt;p&gt;- use a hecking xml parser instead&lt;p&gt;It almost reminds me of a Bill Nye sketch. Teaching through a bit of non-sequitur and absurdism.</text></comment>
<story><title>Is there a regular expression to detect a valid regular expression?</title><url>https://stackoverflow.com/questions/172303/is-there-a-regular-expression-to-detect-a-valid-regular-expression</url></story><parent_chain><item><author>hk__2</author><text>It’s a famous post on StackOverflow, but I don’t find it particularly helpful.</text></item><item><author>abraCadabstrax</author><text>Perhaps the most epic reply I have ever seen on SO.</text></item><item><author>gamache</author><text>I believe Zalgo has the answer to this, via an equivalent question. &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;stackoverflow.com&amp;#x2F;questions&amp;#x2F;1732348&amp;#x2F;regex-match-open-tags-except-xhtml-self-contained-tags&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;stackoverflow.com&amp;#x2F;questions&amp;#x2F;1732348&amp;#x2F;regex-match-open...&lt;/a&gt;</text></item></parent_chain><comment><author>stfwn</author><text>If you were looking for the reason why a regex cannot parse HTML, it is because HTML has matching nested tags and regex parsers are finite state machines (FSM).&lt;p&gt;What this means is that a regex parser is like a goldfish. It only knows about the state it is currently in (what it just read) and which possible states it may transition to (what is legally allowed to come next). The fish never remembers where it was before; there is no option to have the legality of a transition depend on what it read _before_ the current state. But this memory function is a requirement to recursively match opening tags to their closing tags - you need to keep a stack of opening stacks somewhere in order to then cross off their closing tags in reverse order. So regex cannot parse HTML.</text></comment>