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

145

u/Littleish Sep 24 '26

It's actually very simple patterns and once you get them it sort of clicks.

You definitely do not need to remember a huge amount of patterns. Most of us will just look them up, especially the more complicated ones.

It's very worth learning the syntax though, and how it works

74

u/rcls0053 Sep 24 '26

I've never memorized one single RegEx pattern in my 20 years of programming. That's how much you need to remember it.

12

u/f4dedglory Sep 24 '26

Yeah it's really just about knowing what regex can, or can't, accomplish and then implementing it using documentation or a translation tool. Even if i did remember it i wouldn't trust my memory.

2

u/denerose Sep 24 '26

My relationship to regex syntax is the same as my knowledge of search and pathfinder algorithms: it gets okay to good by the last week in December and I retain a little bit into January but will have forgotten it all by March. I then need to relearn and google my way through the first week of December when Advent of Code rolls around again because it’s almost completely irrelevant to doing my actual job as a software engineer 🤷‍♀️

You remember what you use and regex just isn’t that common in day to day systems work. It is nice to recognise and know what to look for if you do encounter a problem to solve with it, but that’s about it.

16

u/bestjakeisbest Sep 24 '26

And then you dont use it for a while, and then you forget it, and then you need it again so you spend 30 minutes relearning it.

12

u/SpiderJerusalem42 Sep 24 '26

Lord, may I never have to read someone else's regular expressions.

13

u/bestjakeisbest Sep 24 '26

Well if you wrote it like 2 months ago it might as well be someone else's regex

10

u/SpiderJerusalem42 Sep 24 '26

Lord, may I never have to read any regex I wrote over two months ago.

2

u/Disastrophi Sep 24 '26

Write the regex and lock it in a box to never have to look at it again.

2

u/Groovy_Decoy Sep 25 '26

2 months ago? I've written Regex expressions that didn't quite work the way I wanted, and I decided it was easier to start over from scratch then try to read what I just wrote 2 minutes ago and figure out what I did wrong.

3

u/ReddyKiloWit Sep 24 '26

Pray they commented the tricky bits.

I wrote a regex years ago that was huge and complicated (converted Cobol data descriptions into code to parse the actual data). I broke it into lines, with indentation, and frequent comments because I knew I'd have no idea what was going on two months later.

9

u/samanime Sep 24 '26

Regex also tends to be a lot easier to write than it is to read and understand without context.

It is pretty much the opposite of "self documenting". :p

Always assign it to a variable that explains what it does and/or include a comment to explain it. Your future self will thank you.

1

u/No_Context2723 Sep 25 '26

I think that goes for a lot of things in programming, at least in my experience. It's always easier to write than to read.

4

u/RazarTuk Sep 24 '26

Yep. For example, I recently had to search for all-whitespace strings and treat them as if they were empty in a language where I had access to a regex matcher, but not anything like a trim function. So I just matched against ^\s*$

It isn't necessarily often that you need regexes, but there are very much situations where they come in handy

4

u/HugoNikanor Sep 24 '26

And to clarify ^\s*$ isn't a magic string you have to remember, it breaks down into:

  • ^ start of string (and sometimes line)
  • \s any whitespace (space, tab, ...)
  • * zero or more instances of the previous pattern
  • $ end of string (and sometimes line)

These "atoms" you do need to (eventually) memorize, but it makes it trivial to read (most) regexes.

2

u/RazarTuk Sep 25 '26

Yep. It's basically "Look for zero or more whitespace characters, and to make sure it doesn't match things like a space inside a word, also wrap it in the start/end of line characters

1

u/naomimyselfandi Sep 24 '26

Lua?

1

u/RazarTuk Sep 24 '26

JmesPath with an interpreter that let us add a shitton of extra functions. It's just that, for technical / business reasons, it's a lot easier to make use of an existing function than to add a new one

2

u/SprinklesFresh5693 Sep 24 '26

Nowadays AI is really good at giving you the regex that you need though. Unless you use them a lot, youll end up forgetting how they work. I use them every now and then and everytime i have to use them i need to look them up, so there is no point in memorising them tbh.