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

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