News for hackers. Stuff that matters.
Re:Natural occurring molecule rivals Ozempic in weight loss,... by crazygringo on Friday March 07, 2025 @10:12AM (#43290785)
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.

Re:Natural occurring molecule rivals Ozempic in weight loss,... by loginx on Friday March 07, 2025 @10:30AM (#43290938)
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.

Re:Natural occurring molecule rivals Ozempic in weight loss,... by yesbabyyes on Friday March 07, 2025 @10:42AM (#43291052)
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

Re:Natural occurring molecule rivals Ozempic in weight loss,... by loginx on Friday March 07, 2025 @10:55AM (#43291194)
Nice roundup, and thanks for sharing that proposal!
Re:Natural occurring molecule rivals Ozempic in weight loss,... by genewitch on Saturday March 08, 2025 @02:17AM (#43298232)
Notepad++ has /x mode as well.

I probably use regexes the most in notepad++

Re:Natural occurring molecule rivals Ozempic in weight loss,... by rtkwe on Friday March 07, 2025 @10:45AM (#43291083)
regex101.com is a lifesaver for parsing esoteric regexes after you've forgotten what they're supposed to do.
Re:Natural occurring molecule rivals Ozempic in weight loss,... by PickledChris on Friday March 07, 2025 @12:31PM (#43292133)
Great advice that is a bit redundant with LLMs, no? They're pretty great at all forms of translation.
Re:Natural occurring molecule rivals Ozempic in weight loss,... by crazygringo on Friday March 07, 2025 @05:48PM (#43295642)
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.