874
top 50 comments
sorted by: hot top controversial new old
[-] frezik@midwest.social 30 points 4 months ago

It helps if you break it apart into its component parts. Which is like anything else, really, but we've all accepted that regexes are supposed to run together in an unreadable mess. No reason it has to be that way.

[-] marcos@lemmy.world 17 points 4 months ago

If they are Perl regexes, like all regexes are supposed to be, you can have non-semantic whitespace and comments.

But if you are using some system that enforces something different, you are out of luck.

[-] frezik@midwest.social 6 points 4 months ago

Not necessarily. For just debugging purposes, you can still break them up to help understand them. Even ignoring that, there are options in languages that don't implement /x.

https://wumpus-cave.net/post/2022/06/2022-06-06-how-to-write-regexes-that-are-almost-readable/index.html

At my company we store our regex in the database with linebreaks in it, but when it's actually called to be used those line breaks are stripped out. That way regex that looks for X can all be all on one line and actually readable.

[-] tyler@programming.dev 10 points 4 months ago

wait... why do you have so many regexes you need to put them in a database???

[-] wise_pancake@lemmy.ca 5 points 4 months ago

The comments flag needs more support.

[-] Kng@feddit.rocks 26 points 4 months ago

I have found chatgpt to be very good at writing regex. I also don't know how to write regex.

[-] Mirodir@discuss.tchncs.de 15 points 4 months ago

In my experience, it is good at simple to medium complexity regex. For the harder ones it starts being quite useless though, at best providing a decent starting point to begin debugging from.

[-] SwordInStone@lemmy.world 7 points 4 months ago

well, you won't get better using chatgpt for it

[-] AngryClosetMonkey@feddit.nl 24 points 4 months ago

Just pop them into regex101 or a similar tool, add sample data, see the mistake, fix the mistake, continue to do other stuff.

[-] WhyJiffie@sh.itjust.works 3 points 4 months ago

Just pop them into regex101 or a similar tool, add sample data, ~~see the mistake, fix the mistake, continue to do other stuff.~~ it works there, pull hair

FTFY

[-] marine_mustang@sh.itjust.works 21 points 4 months ago
[-] gofsckyourself@lemmy.world 30 points 4 months ago
[-] echindod@programming.dev 4 points 4 months ago

This is the one I use! Might have to look at regexer though

[-] exu@feditown.com 20 points 4 months ago

I usually do

# What we are doing (high level)
# Why we need regex
# Regex step by step
# Examples of matches
regex

And I still rewrite it the next time

[-] InnerScientist@lemmy.world 16 points 4 months ago* (last edited 4 months ago)
// abandon all hope ye who commit here
(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])

Edith: damit, Not the first to post this abomination

[-] Skyrmir@lemmy.world 16 points 4 months ago

Never debug regex, just generate a new one. It's not worth the hassle to figure out not only what it does, but what it was meant to do.

Better yet, just write it out in code, and never use regex. Tis a stupid thing that never should have been made.

[-] potustheplant@feddit.nl 16 points 4 months ago* (last edited 4 months ago)

Hard disagree. The function regex serves in programs like Notepad++ can't be easily replaced by "writing it out in code". With a very small number of characters you can get complex search patterns and capturing groups. It's hard to read but incredibly useful.

[-] kunaltyagi@programming.dev 6 points 4 months ago* (last edited 4 months ago)

Can't upvote twice, have a low effort comment instead

[-] chad@sh.itjust.works 2 points 4 months ago

I fall in the abandon it camp. Code is read way more times than it is written. I'd rather read an algorithm that validates input than read a regex that validates input.

[-] potustheplant@feddit.nl 4 points 4 months ago

You're discussing a completely different use case from what I said. RegEx can be increidbly useful but it's not always the only/best option.

load more comments (10 replies)
[-] sakodak@lemmy.world 4 points 4 months ago

Regex is a write only language.

[-] Hoimo@ani.social 2 points 4 months ago

I love regex and I use it a lot, but I very rarely use it in any kind of permanent solution. When I do, I make sure to keep it as minimal as possible, supplementing with higher level programming where possible. Backreferences and assertions are a cardinal sin and should never be used.

[-] AI_toothbrush@lemmy.zip 15 points 4 months ago

Me checking my own docs: "this is some voodoo shit, idk how it works"

[-] MTK@lemmy.world 11 points 4 months ago

Downvoted so that everyone can know I'm cool since I understand regex better than the idiot who made that meme.

[-] fmstrat@lemmy.nowsci.com 9 points 4 months ago

I know I'm weird, but I love regex.

[-] verstra@programming.dev 8 points 4 months ago* (last edited 4 months ago)

If I have a complex regular expression to code into my app, I write it in pomsky, then copy paste the compiled regex to my source file, but also keep the pomsky source nearby. Much more maintainable.

[-] vk6flab@lemmy.radio 7 points 4 months ago

There are a few online regex testing tools that will analyse your efforts and give you the opportunity to provide sample data.

[-] DrDeadCrash@programming.dev 6 points 4 months ago

Aziz! LIGHT!

[-] psmgx@lemmy.world 6 points 4 months ago

Ohhhhh it was this extra '

[-] kamen@lemmy.world 4 points 4 months ago

There are no bugs, it's just not doing what you expect it to be doing...

... which, now that I think of it, can be said about all software in general.

[-] Prime_Minister_Keyes@lemm.ee 3 points 4 months ago
[-] disguy_ovahea@lemmy.world 2 points 4 months ago

I think he found the Road Runner.

load more comments
view more: next ›
this post was submitted on 14 Feb 2025
874 points (100.0% liked)

Programmer Humor

24373 readers
681 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS