Live data from Hacker News

Regex Isn't Hard (2023)

timkellogg.me

81–90 of 109 posts

Re: Regex Isn't Hard (2023)

#81

This is truly one thing AI solved. Hard to write, easy to test. No one needs to learn this convoluted syntax in the future and we're all better for it.

How would you know if a regex is correct if you dont understand it?

You have test strings covering all cases and they match accordingly? The same way you'd know when writing manually.

Re: Regex Isn't Hard (2023)

#82
post #40

Earlier quoted context omitted.

IMO it’s a “language” you need to understand in order to use. Just like you wouldn’t copy/paste any random snippet into your source code if you don’t understand exactly what it does. I see a lot of broken regex at work from people who use regular expressions but don’t understand them (for various reasons). It used to come with a “found this on stackoverflow”-excuse, but mostly now it’s “AI told me to use this” instea…

yeah programmers famously understands all the random boilerplate incantations they copy past in their code to get things going. totally definitively

I know some people consider this fine. I do not. The fact that the world is not ideal does not mean that we cannot continue to improve things.

Re: Regex Isn't Hard (2023)

#84
post #47

I’m a fan of regular expressions, though I understand why many people wince at the sight. You should avoid showing them to a non-programmer who is interested in learning to code, because they’ll immediately fear programming is intractable. Even as much as I like regex, I wouldn’t recommend this post. One reason is the code style is too close to regular text: > a matches a single character, always lowercase a. That se…

I've also seen people use `[\s\S]` to match all characters when they couldn't use `.`.

This is a common approach when the regex needs to match any character including newlines; `.` often doesn't.

Re: Regex Isn't Hard (2023)

#85
post #47

I’m a fan of regular expressions, though I understand why many people wince at the sight. You should avoid showing them to a non-programmer who is interested in learning to code, because they’ll immediately fear programming is intractable. Even as much as I like regex, I wouldn’t recommend this post. One reason is the code style is too close to regular text: > a matches a single character, always lowercase a. That se…

I've also seen people use `[\s\S]` to match all characters when they couldn't use `.`.

I generally use `[^]`

Also you can use . with the dotAll /s

Re: Regex Isn't Hard (2023)

#88

Earlier quoted context omitted.

How would you know if a regex is correct if you dont understand it?

You have test strings covering all cases and they match accordingly? The same way you'd know when writing manually.

Covering all cases? How would that be possible? Even if we only consider ASCII strings, there are 16.000 possible two-character strings, 2 million possible three-character strings and so on.

Re: Regex Isn't Hard (2023)

#90

Earlier quoted context omitted.

My problem is that regexes are write-only, unreadable once written (to me anyway). And sometimes they do more than you intended. You maybe tested on a few inputs and declared it fit for purpose, but there might be more inputs upon which it has unintended effects. I don't mind simple, straight-forward regexes. But when they become more complex, I tend to prefer to write out the procedural code, even if it is (much) lo…

> I tend to prefer to write out the procedural code, even if it is (much) longer in terms of lines. This might work for you, but in general the amount of bugs is proportional to the amount of code. The regex engine is alredy throughly tested by someone else while a custom implementation in procedural code will probably have bugs and be a lot more work to maintain if the pattern changes.

In general, the correctness of the code is proportional to its readability.

I also prefer procedural code instead of regexes.

Post reply on HN