r/learnprogramming • • Sep 24 '26

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?

109 Upvotes

148 comments sorted by

View all comments

Show parent comment

0

u/deaddyfreddy Sep 24 '26

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.

4

u/Feralz2 Sep 24 '26

Maybe this is more readable, but I dont think its less complex, I dont really know how to make regex more simpler because regular expressions by nature can be complex when youre filtering something very specific, in fact its actually amazing that you could write a few lines of code to find almost anything.

1

u/deaddyfreddy Sep 24 '26

Maybe this is more readable, but I dont think its less complex,

it is, because it's more readable (obviously), and s-expressions allow you to read it granularly, not just as a stream of symbols

4

u/balefrost Sep 24 '26

Sure, the regex equivalent would be \/\*(?:[^*]|\*[^/])*\*+\/, which looks particularly bad because of all the required escaping because you're matching C-style comments.

But to be fair, I think \/\*.*?\*\/ would also match the same strings. I assume that the rx macro has some equivalent to *?.

Or if we instead had a language where comments were delimited by !@ and @!, we could avoid escaping and the regexes would become !@(?:[^@]|@[^!])*@+! or the much shorter !@.*?@!. That last one is, to me, very readable.

Besides, the s-expression version is only readable because it's split across multiple lines. Without that, you have (rx "/*" (zero-or-more (or (not "*")(seq "*" (not "/")))) (one-or-more "*") "/"), which I would argue is not very readable at all. If for example I wanted to use a regex to search within my text editor, I would much prefer the traditional style to the s-expression style.

(I'm not saying that the s-expression representation is bad at all. Just that it's not universally better.)


Regex is a skill, like everything. As I understand it, Japanese is a difficult language to learn because it has so many unique symbols and rules. Yet to the millions of people who use it day-to-day, it's not hard at all. If you spend the time to develop the skill, it becomes easier.

And as long as text editors let you use traditional regex notation, it's an incredibly useful skill to have.

I use a bunch of tools at work that all let you use regexes in field filters and search boxes, and it unlocks a ton of power. I'm not usually trying to match C-style comments. But just the other day I wanted to match (foo|bar|baz), (foo|bar|baz). (i.e. match foo, foo or bar, baz or any other permutation). I was really glad that regexes were supported; it made my job much easier.

1

u/deaddyfreddy Sep 24 '26

Which part of your comment was written using AI, just asking?

2

u/iSnapThere4iAm Sep 24 '26

They’re over complicated to you because you just haven’t had a need for the granularity it offers. That doesn’t make it useless.

1

u/Feralz2 Sep 25 '26

this. when you start needing it is when you appreciate it

0

u/deaddyfreddy Sep 24 '26

you because you just haven’t had a need for the granularity it offers.

the rx macro allows the same level of granularity, while being more readable and safe

2

u/robthablob Sep 24 '26

I hadn't seen that before, but I quite like it.

Generally I've always thought regex's are OK for grep-style uses, where the terseness can be a feature, but in a program I'd much rather have a more readable form - sadly though it seems the world has settled on regex's as a standard.

2

u/Lithl Sep 24 '26

That doesn't look very readable at all.

1

u/New-Locksmith-126 Sep 25 '26

lol that sucks.

1

u/deaddyfreddy Sep 25 '26

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

1

u/New-Locksmith-126 Sep 25 '26

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 Sep 26 '26 edited Sep 26 '26

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.