6 ms·
I never understood why so many developers have problems with Regex. In my experience, it's difficult because most people avoid it, once you use it enough times
by kidsil 11y ago
I never understood why so many developers have problems with Regex.
In my experience, it's difficult because most people avoid it, once you use it enough times the logic makes perfect sense.
- nicwest 11y agoit's a transferable skill as well. The engine/syntax might differ a little language to language, but the core concepts remain the same.
- roel_v 11y agoI think it's because few take the time to actually understand them before using them, and usually suffice with cobbling together a few expressions from 'tutorials' of varying quality. Reading the Friedl book at least once is essential. Then again, what do we expect when our field is filled with people whose 'education' consists of a 6-week 'hacker bootcamp' and a few weekends of watching Youtube videos... (also, too many kids on my lawn etc)
- effie 11y agoThat's a patronizing comment. Often the very education may a stumbling block in the process of learning. Most users of regexps or any other device do not need to read a thick theory book on it. Cobbling together expressions from online tutorials is a perfectly good way to learn effectively. Admittedly, it is not the best way to write production code, but there are often economic considerations that override the expert's desire for perfect design and implementation.
- roel_v 11y ago"Often the very education may a stumbling block in the process of learning." People don't want to learn because it's hard? "Most users of regexps or any other device do not need to read a thick theory book on it." Which is why they should at the very least read Friedl. "Cobbling together expressions from online tutorials is a perfectly good way to learn effectively." No it's not, otherwise we wouldn't have so many bad regexes and people asking silly questions about them, would we? "Admittedly, it is not the best way to write production code, but there are often economic considerations that override the expert's desire for perfect design and implementation." Nobody's talking about 'perfect design and implementation', just 'not be a moron' level. Because car analogies are everbody's favorite, let me use one here: no-one is saying that one should have a Formula 1 licence before driving (using regular expressions); just to have more than 20% vision in both eyes and not to drive after drinking 5 beers while texting your wife that you're on your way. Which is the car-driving equivalent of the majority of regex uses in the wild.
- effie 11y ago> People don't want to learn because it's hard? There is truth in that, but I meant to address your comment about other people's lack of education. Education (certificate granted by teaching authority), how good soever it may be, is not necessary to have knowledge or skill. Similarly, reading Friedl how good soever his work is, is not necessary to learn or use regular expressions.
- banthar 11y agoI do not know if it's correlation or causation, but almost all code using regular expressions I encounter is horribly broken. 1. Regular expressions are often used instead of existing parsers. XML, CSV, file paths, URIs etc. all already have fast, well tested and correct parsers. 2. Often the thing worked on should not be a string in the first place. For example comma separated string is used instead of a list and regular expression emulate list operations. 3. Some things could be processed much easier with different tool - for example recursive descent parser. Yet the developer still tries to parse arithmetic expressions with a hammer. 4. They are often developed via trial and error. They are either first thing which worked for a simple case or are 10 line monsters riddled with exceptions from exceptions. 5. They are often part of hacks and workarounds. For example User-Agent is matched to work around bugs in browsers. 3. They give very limited feedback to user. There is either a match or no match. There is no way to tell what and where is broken. There are valid use cases for regular expressions. It's even possible to write correct code with them. It's just a rare sight.
- ivanhoe 11y agoMany of these points are correct, but a little comment on #1: Often you don't care about the whole structure, you just need some small piece of data from a middle of a huge document. One common example is a spider that collects a price of some product from a 200+KB webpage. You just need those few digits and don't care about the head or title or the structure of dom or anything else. In such cases (and it's very common task for people working on data extractions) no complex parsers can ever compete with the regexp in terms of speed and memory footprint. And if you need to parse a few millions of products that performance gain is a huge deal. So don't underestimate the power of regexp when properly used...
- banthar 11y agoThat is fine for throwaway scripts. But such "perl duct tape" is not 100% accurate and will break for no reason. There is no place for such solutions in reliable and maintainable software.
- 11y ago