3 ms·
Please, if you write regexes like these, comment them... A great way to do it is to split them up by concatenating them across a bunch of lines, and put a brie
by crazygringo 2y ago
Please, if you write regexes like these, comment them...
A great way to do it is to split them up by concatenating them across a bunch of lines, and put a brief explanation at the end of each non-obvious part (to the right, on the same line).
Plus that also lets you indent within nested parentheses, making it that much more understandable.
I'm baffled when I come across a file like this where the code itself is heavily commented, but a gnarly regex is not. Regexes are not strings, they are code -- and with their syntax, they need comments even more.
- loginx 2y agoOne thing I always appreciated about CoffeeScript was its [heregex syntax](https://coffeescript-cookbook.github.io/chapters/regular_expressions/heregexes https://coffeescript-cookbook.github.io/chapters/regular_exp...), which made regexes clear and easy to document. Many CoffeeScript constructs were adopted into JavaScript and TypeScript, but unfortunately, heregexes weren't among them.
- yesbabyyes 2y agoI agree. I'm pretty sure jashkenas got it from Ruby, which got it from Perl, with the `/x` flag (for extended regular expressions). Later languages have added support for the `x` flag, including C# and Rust. There is also a stage 1 proposal for JavaScript: https://github.com/tc39/proposal-regexp-x-mode https://github.com/tc39/proposal-regexp-x-mode
- rtkwe 2y agoregex101.com is a lifesaver for parsing esoteric regexes after you've forgotten what they're supposed to do.
- PickledChris 2y agoGreat advice that is a bit redundant with LLMs, no? They're pretty great at all forms of translation.
- crazygringo 2y agoFirst, I don't want to waste time feeding something into an LLM that should be commented in the first place. Second, not at all. An LLM can tell you how the regex works (hopefully). It can't tell you what each piece means in terms of the program's logic. Or at least not always and not reliably.