Hacker News new | past | comments | ask | show | jobs | submit login

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.




One thing I always appreciated about CoffeeScript was its [heregex syntax](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.


I 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


Nice roundup, and thanks for sharing that proposal!


Notepad++ has /x mode as well.

I probably use regexes the most in notepad++


regex101.com is a lifesaver for parsing esoteric regexes after you've forgotten what they're supposed to do.


Great advice that is a bit redundant with LLMs, no? They're pretty great at all forms of translation.


First, 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.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: