10 ms·
For anyone who doesn't get the reference: https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 htt
by foxbarrington 5y ago
For anyone who doesn't get the reference: https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 https://stackoverflow.com/questions/1732348/regex-match-open...
- TacticalCoder 5y agoOh you beat me to it.
- Rendello 5y ago> regex will consume all living tissue (except for HTML which it cannot, as previously prophesied) This part gets me every time!
- deleted 5y ago[deleted]
- isoprophlex 5y ago> The <center> cannot hold it is too late. Never fails to make me chuckle
- Anthony-G 5y agoNice reference to Yeats’ poem, “The Second Coming”. I guess the infamous Stack Overflow answer is a modern form of poetry.
- jonathanoliver 5y agoWhere I work, we have this post as part of our software developer on-boarding experience. It's required reading.
- goto11 5y agoHave any of the developers noticed the fundamental fallacy in the answer? The question is not about parsing, it is about recognizing a token in a string, which is exactly what regular expressions are designed for.
- Dylan16807 5y agoWhich is a fun reference but this isn't exactly parsing and I think using one regex to process escapes would actually fix the problem. Edit: The function in the fix does in fact appear to be a straightforward implementation based around a regex. https://github.com/mathiasbynens/he/blob/master/src/he.js https://github.com/mathiasbynens/he/blob/master/src/he.js
- yarcob 5y agoI think the Zalgo answer is an wonderful example of people thinking they are smarter than they really are. Spot the error: 1) HTML is not a regular language 2) Regular expressions can only parse regular languages 3) Regular expressions can not be used to to parse arbitrary HTML documents 4) If you try to use regular expressions to parse HTML documents, you are doing it wrong The error is between step 3 and 4: It's true that you can't write a regex that parses arbitrary HTML. However, very often we do not have to deal with arbitrary HTML. Very often we have to deal with documents that only use a subset of HTML and they can be parsed by regular expressions just fine.
- bawolff 5y ago> 2) Regular expressions can only parse regular languages This is false, regular expressions as popularly implemented can parse non-regular languages. For example, /^(.*)\1$/ is a non regular language.
- exmadscientist 5y agoStrictly (pedantically) speaking, using a backreference turns it from a "regular expression" into a "regex", regexes being a superset of regular expressions that are no longer strictly limited to being regular. Isn't terminology great?
- jjtheblunt 5y agoIt's true, not false. You refer to a misleading overloading of the term when referring to the stack machines needed to realize 'regular expressions with backreference memories', which expands the language class to which they correspond. Citation : https://www.amazon.com/Introduction-Automata-Languages-Computation-Addison-Wesley/dp/020102988X https://www.amazon.com/Introduction-Automata-Languages-Compu...
- goto11 5y agoA rebuttal here: https://www.cargocultcode.com/solving-the-zalgo-regex/ https://www.cargocultcode.com/solving-the-zalgo-regex/
- sobellian 5y agoI somewhat disagree that the famous answer is an instance of cargo-culting. Even if not for entertainment it's probably more valuable than literally answering the question posed. Let's say that you now have a method that extracts tags in the manner prescribed... now what? I'd bet the OP was only trying to write that method as a subprocedure for some larger task much more efficiently dealt with another way. In fact if you look at that poster's question history around that time, it's apparent that they were trying to render potentially-malformed XHTML.
- enriquto 5y agoregardless of cargo-culting or not, the rebuttal explains why the famous answer is utterly wrong: the problem can be solved by regular expressions and it cannot be solved using an xml parser. While the answer is funny and probably good general advice, it is incorrect.
- avmich 5y agoYou can't really argue with a mathematical theorem.
- rblatz 5y agoThey aren’t asking to parse all valid xml. They wanted to parse a small known subset. Regex can be an excellent tool in that situation.
- dragonwriter 5y ago> They aren’t asking to parse all valid xml. They wanted to parse a small known subset. Regex can be an excellent tool in that situation. Rarely better than using an existing general XML parser and then validating the additional constraints, especially in terms of implementation and maintenance cost.
- mintplant 5y agoWorth mentioning that Zalgo predates the StackOverflow post (a̷nd̡ t̕h͟i̡s u̕niver͠s͠e̵). https://en.wikipedia.org/wiki/Zalgo_text https://en.wikipedia.org/wiki/Zalgo_text