5 ms·
I had one class where we had to build a multi threaded search engine in java and parsing HTML with regex was a requirement. Regex was the downfall of about 50%
by capkutay 14y ago
I had one class where we had to build a multi threaded search engine in java and parsing HTML with regex was a requirement. Regex was the downfall of about 50% of the class and the majority of the students who did well still had slight issues with their HTML parsing. Moral of the story is that regex is a poor solution for HTML. Not to mention, hours debugging regex is one of the least meaningful or rewarding experiences you can have as a programmer.
- pjscott 14y agoThe obvious workaround for that broken requirement is to use regular expressions to handle the tokenization, and then write a simple recursive descent parser on top of that. Even if this is playing fast and loose with the requirements, it would work out fine, and you would almost certainly get a good grade if you explain why you did it.
- pidge 14y agoYes, this ^ Because, no, you can't parse XHTML with regex. As easily shown by the pumping lemma and all that jazz. But, there's no freaking reason why you can't tokenize an XML start tag with a regex! In fact, you'll probably find that most uses of parsers in real life have regexes to tokenize down at the level that they can handle, before using a parser on the resulting tokens for the part that actually needs to be a CFG (among other reasons, because a compiled FSM is a lot faster than even a limited LALR parser). Looking at this specific example, we can refer to the definitions for start tags [1] and empty element tags [2] in XML, and see that all their constituent rules form a regular language (if you don't believe me, it's not too hard to go check for yourself). So, especially since the orignal question doesn't even mention 'parsing', can we all please just shut up? (unless you actually want to figure out the horrible mess necessary to define a regex from the spec :P ) 1. http://www.w3.org/TR/xml11/#sec-starttags http://www.w3.org/TR/xml11/#sec-starttags 2. http://www.w3.org/TR/xml11/#dt-eetag http://www.w3.org/TR/xml11/#dt-eetag
- Evbn 14y agoDid the other 50% succeed by telling the prof they got that he was joking?
- chimeracoder 14y ago> the majority of the students who did well still had slight issues with their HTML parsing. I'd like to hire the minority students, since it seems that they quite literally accomplished the impossible!
- capkutay 14y ago> the majority of the students who did well still had slight issues with their HTML parsing. There were a few students who actually passed all of our professor's test cases (at least that's what they claim).