4 ms·
>Enter regular expressions. I have never, ever spontaneously composed a regex. I have to be very slow and deliberate. I have to recall the precise definition of
by _dain_ 4y ago
>Enter regular expressions. I have never, ever spontaneously composed a regex. I have to be very slow and deliberate. I have to recall the precise definition of every character in the regex as I read or write it. Sometimes I stare at a regex for 2 minutes, look up some operator, stare again for 2 minutes, refactor it. Finally I understand it and can now apply the regex spontaneously to whatever environment I'm working in. It always feels like first principles architecture, never like flow. This is odd, as regex is like a giant shorthand engine.
I can do simple regexes at pretty close to the speed of thought. e.g. here's one to recognize the "arthurofbabylon 20 minutes ago" bit at the top of HN comments:
(-?\d+ points? by )?[a-zA-Z0-9_-]* ((just now)|(\d+ (minutes|hours|days|months) ago)|(on (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d{1,2}, \d{4}))
I typed that out pretty much all in one go, just had to look up different ways the HN date thing gets formatted. I put it in an online checker and it seemed to work on some examples, just had to fix a mismatched parentheses pair.
Not saying it's completely correct (probably someone will reply with a counterexample or problem with it) but I think it's a demonstration that regex fluency is possible. I do this kind of thing on the command line a lot, interactively to figure stuff out from logs and so on. For production code I'm more circumspect.
- pygy_ 4y agoRegExp is optimized for typing, not for reading. Trouble comes when one need to update or debug a complex RegExp. The one you posted is not that complex.
- ttfkam 4y agoBut isn't that true of most programs once they reach a certain level of complexity? Especially after they've been optimized? Eventually one relies on tests to ensure your changes don't introduce bugs and unwanted side effects. Sadly, most folks don't bother to write tests for their regexes.
- ttfkam 4y agoJust to chime on among the naysayers, I just read your regex at "the speed of flow". Made perfect sense to me. (I'd have used non-matching groups, but that's a very minor nit.) The equivalent code to parse this is much longer and arguably to me not really easier to grok. (Unless of course you regularly avoid regex, in which case it will always remain hard.) I'd say the hardest thing for beginners is the character class and how it matches EXACTLY ONE LETTER without a modifier. Everything matches exactly one letter without a modifier. It seems intuitive to me, but obviously a lot of folks have trouble with it, so I accept that we are outliers. Reminds me a bit of folks who steadfastly avoid SQL. You have to change the flow in your brain; there's more than one flavor of flow. For regex, it's the watching for single character match with optional modifier. For SQL, it's thinking in terms of sets, not instructions. If regexes make you feel stupid, lean into it instead of recoiling from it. That's where the learning happens: immersion into the uncertainty until it becomes familiar.
- _dain_ 4y ago>Reminds me a bit of folks who steadfastly avoid SQL. You have to change the flow in your brain; there's more than one flavor of flow. For regex, it's the watching for single character match with optional modifier. For SQL, it's thinking in terms of sets, not instructions. I'm one of those people, SQL just doesn't fit in my brain right :(
- ttfkam 4y agoNothing logical ever does. The human brain is a pattern recognition engine. You just haven't had enough time with these particular patterns. It's like spoken language; it just takes time, exposure, and practice. Just find the right strategy for you to internalize the patterns. Maybe a game? https://mystery.knightlab.com/walkthrough.html https://mystery.knightlab.com/walkthrough.html