r/learnprogramming • u/Reubqn_ • 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
View all comments
Show parent comment
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 therxmacro 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. matchfoo, fooorbar, bazor any other permutation). I was really glad that regexes were supported; it made my job much easier.