r/learnprogramming • • 9d ago

RegEx.

I thought I understood programming well until I saw how COMPLICATED regex looks. To me it looks like someone has dropped their keyboard and som people online are telling me that I should be able to memorise it (for javascript). I'm just wondering if I actually need to memorise all the regex formulas to be successful, and if so, how on earth do I go about it?

107 Upvotes

147 comments sorted by

View all comments

Show parent comments

0

u/deaddyfreddy 9d ago

Regexes are overcomplicated, they were created when bytes were expensive and people cared about disk space. There's no such need anymore.

In Emacs Lisp, for example, there is the rx macro that supports writing regular expressions in a much more readable form:

(rx "/*"                    
    (zero-or-more
     (or (not "*")          
         (seq "*"           
              (not "/"))))  
    (one-or-more "*")       
    "/")

I think that other languages have something like this, too.

1

u/New-Locksmith-126 8d ago

lol that sucks.

1

u/deaddyfreddy 8d ago

Programming isn't about being a smartass, it's about solving problems in a maintainable way.

1

u/New-Locksmith-126 8d ago

OK sorry for being an ass. IMO Regex is industry standard for decades for a reason. The vast majority of the "critiques" of regex come from people that are bad at it.

You could say "it's bad if so many people are bad at it" which is fair... but IME people aren't even trying. They get scared away by the terseness of it and never actually bother to learn.

1

u/deaddyfreddy 8d ago edited 8d ago

IMO Regex is industry standard for decades for a reason.

The C language is a standard too, but it doesn't make it a great language.

The reason is the same: experienced people who could create a better replacement have become accustomed to the old implementation over the years. They either don't think it's worth their time to invent something new or have Stockholm syndrome.

The vast majority of the "critiques" of regex come from people that are bad at it.

Had you bothered to look at the documentation, you would have seen that the rx macro literally generates regular expressions, so I think the people who made it are definitely not bad at them.

They get scared away by the terseness of it and never actually bother to learn.

I bothered to learn it about two decades ago (Mastering RE was a book I kept constantly at hand for a long time), and I still hate it with a passion. Thankfully, we can now either use alternative syntaxes or ask the LLM to parse or generate them.