4 ms·
> The question is about finding opening tags in XHTML using a regular expression Bzzzt, wrong, sorry! The question is about finding open tags in the presence
by inopinatus 5y ago
> The question is about finding opening tags in XHTML using a regular expression
Bzzzt, wrong, sorry! The question is about finding open tags in the presence of XHTML self-closing tags. That difference alone places these interpretations gulfs apart. But there’s more: it does not specify that the input document is even XHTML, only that XHTML-style self-closing elements may be present. In fact the original question was barely
minutes old and tagged merely “regex” when that famous answer was written in 2009; the question was not tagged with “xhtml” until 2012, and not by the original author either.
Revealingly, then, if we review the broader context (i.e question history) of the original question author, it’s clear that yes indeed they were trying to fix a malformed document, and in particular to normalise it into XHTML, with focus on fixing up any so-called “dangling tags”. For this task, the suggestion of “use a parser” is indeed sound advice.
The real moral here is, don’t be a jerk about the precise semantics of a question, look at what the person needs, and help them ask better questions.
Otherwise, you’re just gonna discover that there’s always a bigger jerk, and they’re on Stack Overflow, moderating your stuff.
- swiley 5y agoHere's the original text for reference: Locked. Comments on this question have been disabled, but it is still accepting new answers and other interactions. Learn more. I need to match all of these opening tags: <p> <a href="foo"> But not these: <br /> <hr class="foo" /> I came up with this and wanted to make sure I've got it right. I am only capturing the a-z. <([a-z]+) *[^/]*?> I believe it says: Find a less-than, then Find (and capture) a-z one or more times, then Find zero or more spaces, then Find any character zero or more times, greedy, except /, then Find a greater-than Do I have that right? And more importantly, what do you think? html regex xhtml Share Improve this question edited May 26 '12 at 20:37 community wiki 11 revs, 7 users 58% Jeff
- inopinatus 5y agoNot sure which revision that's pasted from, but it is not the original text, which can be found at https://stackoverflow.com/revisions/1732348/1 https://stackoverflow.com/revisions/1732348/1 and I strongly advise against neglecting the title; doing so is how some folks blunder into misinterpretation, since the wording of that title is telegraphing a quite different underlying need to "please solve this regex puzzle".
- DangitBobby 5y agoReading both, I see little to no difference. Is there an important change between the two revisions that I am missing? Also, my reading of the title does not, in fact, telegraph anything other than "help me with this regex puzzle."
- inopinatus 5y agoI recommend consulting my other remarks within this topic, but on this specific point: The title's inconsistency with the body text, i.e. "open tags" vs "opening tags", is especially and immediately notable because they are (in context) grammatically interchangeable but have dissimilar meanings. This is immediately suggestive of (but not diagnostic of) a writer revealing context and then switching to detail. As a longtime reader of requirements documents and S.O. questions, a mental flag to check the intended meaning of both is already raised at this point. The reference to XHTML is ambiguous, since it speaks to working around XHTML that is already present, rather than defining an input or output document, which means this is something thinking at the character level rather than in terms of DOM. This impression is verified by the comparison pairs in the question body, leaving the essential context question of what are the desired inputs and outputs?" glaringly unanswered. The early construct, "I need to ..." is a secondary flag that immediately reinforces the likelihood of a critical gap in context, since it does not explain why the need has arisen. To anyone familiar with the structure & semantics of the HTMLs, the omission of any mention of tag closing, having discussed "opening tags", boosts the sense that something relevant is missing from the question. The obviously flawed regular expression then attracts a "probable novice" qualifier, which is amplified by both the closing "what do you think?" and the original's emoticon. At this point, we're about ten seconds into the read-through and there's already a ton of labels pointing to a beginner who may be conflating dissimilar concepts and (through inexperience) choosing the wrong tool for the task, whatever that may be. Another few seconds to review comments, at which point it appears that author does understand the distinction between an "open tag" and an "opening tag", and this reweights the XHTML reference in the title toward being a need to output strict XHTML. Given their apparent beginner standing it's likely this is either their first S.O. post, or one of slightly too many, and a check in their profile for proximate questions immediately reveals the latter case. References to balancing of "dangling tags" abound, collapsing all preceding problem variants and likelihoods into the only focused understanding that fits: it's a PHP novice, bright but inexperienced, who wants to normalise a nonconforming document into XHTML by balancing the tags, and that the mostly likely use case is injecting an existing HTML fragment into a RSS XML feed. Elapsed time ca.90 seconds. Another minute to scroll through surrounding Q&A materials, to allow alternative options to appear (they don't). A few seconds for a chuckle at that old friend of an amusing answer, and recognise that the advice within is sound: what they really need is, indeed, a parser. Then, a pause to consider a contrasting notion, that what they're trying to do is write a parser. Consider evidence that author has taken at least one step towards inventing stacks on their own from first principles; prospect loses to existing top answer because the experience level required to write parsers cannot, ultimately, be acquired from a single S.O. question. Last but not least; the final boss: convincing the chorus of Hacker News to accept this interpretation. That takes much longer.
- deleted 5y ago[deleted]
- IshKebab 5y agoRead the regex. It handles self-closing tags fine. Also you're doing the intensely annoying thing that lots of StackOverflow people do of imagining that the asker really wanted to ask a different question. It happens sometimes. But you shouldn't just jump in and assume that they don't know what they want and you're so much smarter than them so you know what they really want. Offer additional answers if you want, but answer the question they asked first. (Sorry, pet peeve.)
- inopinatus 5y agoYou're not the first to take that line, so I'll refer you to my previous observations: https://news.ycombinator.com/item?id=27097403 https://news.ycombinator.com/item?id=27097403 There's no wild assumption going on here. I just bothered to keep reading, very carefully, everything the original author actually wrote. Then, please, further reflect that Stack Overflow is not Codewars; it is a forum for practical, focused, and relevant problem-solving advice, and at its best the moderation and answer processes help folks to iteratively revise and improve their questions. A crucial step is, therefore, analytically clarifying both the parameters and the intended outcome. Contextualisation and focusing of requirements is a familiar and essential skill for any programmer being handed a requirements statement, and answering S.O. questions involves the same exercise, just in vignette. The handling of this question was not, in this sense, the best. But expecting anything less, would just be a load of fizzbuzz.
- IshKebab 5y ago> There's no wild assumption going on here. I just bothered to keep reading, very carefully, everything the original author actually wrote. That doesn't excuse it. You're still inferring stuff that wasn't asked in the actual question. What happens when someone else comes along who really does want to use a regex? They now have a question without the correct answer and they can't even ask the question themselves because it will be closed as duplicate! Probably by people like you.
- deleted 5y ago[deleted]
- a1369209993 5y ago> For this task, the suggestion of "use a parser" is indeed sound advice. Perhaps technically, but it's also useless advice because a parser does not exist for their particular flavor of malformed XHTML. XHTML parsers parse XHTML, which you yourself have said it wasn't: > they were trying to fix a malformed document So in the absence of a reference to a particular malformed-XHTML-recovering parser (which may or may not work on the specific input they have, but "try this thing" is at least actionable advice), "use a parser" amounts to "write a entire parser yourself, then use it". > don't be a jerk about the precise semantics of a question, look at what the person needs Pot, kettle.
- deleted 5y ago[deleted]
- inopinatus 5y ago> "use a parser" amounts to "write a entire parser yourself, then use it" "Use a parser" is a common answer, besides being the accepted one, and with good reason: it'll work. The world is not short of HTML parsers (although, who knows, perhaps PHP may have been short of very good parsers back in 2009). Whether they use regular expressions for tokenizing is an internal detail. Serializing XML from the resulting memory structure, DOM or otherwise, closes the loop, and this remains a conventional and commonplace means to normalize some incoming HTML-like mush into something that can be spliced/interpolated into XML and a strict receiver will probably accept it. > Pot, kettle. Oh look, personal abuse! Good-day to you, too.
- 1vuio0pswjnm7 5y agoWhat if we looked at the XHTML parsers trusted by the people who mindlessly dismiss the utility of regular expressions and found they were constructed using a lexer that relied on regular expressions.
- deleted 5y ago[deleted]
- inopinatus 5y agoWell, since I've learned at least four assembly languages, and wrote my first toy lexer at least 25 years ago, probably in Standard ML, by this standard I seem qualified to comment. It doesn't matter what the parser looks like under the bonnet. What matters is the utility it provides. One might otherwise similarly offer the advice to mine your own copper and grow your own silicon, these being equally essential activities for anyone seeking the ultimate in mechanical sympathy.
- goto11 5y agoThe problem is the answers saying "this is not possible". The OP asks a perfectly reasonable question. The answers assume the OP actually meant to ask a different question, and they they ridicule the OP for this imagined question. The question they imagine the OP asks was: "How do I parse an arbitrary HTML document into an element-tree using only a single regular expression and not using any auxiliary data structures, not even the call stack?" Yes, this is indeed not possible, given the limitations of vanilla regular expressions. But that was not the question asked. Of course "use a parser" is perfectly sound advice (if a parser exists which can solve the OP's problem). But saying what the OP is attempting to do (tokenize xhtml) is impossible is absurd, since then it would also be impossible to write a parser!
- inopinatus 5y agoThey're trying to tokenize HTML into two arrays, one being an array of opening tags and one of closing tags, with the hope being to pairwise compare and reconcile the elements of these arrays. The necessary clarification appears further down the page. As for whether it's a reasonable question; as written I beg to differ, it's the opposite, since it does not convey their problem except by a very careful and nuanced reading. The proper response of the S.O. community should've been to aid the OP in clarifying their intentions and amending the question accordingly to bring focus onto their actual problem. That is not what happened. Instead they are indeed on the receiving end of some ridicule, which is shameful, but the advice of "use a parser" would still likely have been a top answer in some fashion had the OP's scattered ancillary questions and clarifications been incorporated. Sadly, only one correspondent of many seems to have seen fit to ask.