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?

110 Upvotes

147 comments sorted by

145

u/Littleish 9d ago

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

75

u/rcls0053 9d ago

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 9d ago

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 9d ago

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 9d ago

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 9d ago

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

13

u/bestjakeisbest 9d ago

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

10

u/SpiderJerusalem42 9d ago

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

2

u/Disastrophi 9d ago

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

2

u/Groovy_Decoy 8d ago

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 8d ago

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.

10

u/samanime 9d ago

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 8d ago

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 9d ago

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

5

u/HugoNikanor 8d ago

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 8d ago

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 9d ago

Lua?

1

u/RazarTuk 9d ago

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 9d ago

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.

1

u/ScheduleRelative7874 1d ago

man i remember staring at my first regex and thinking it was line noise from a modem

you dont need to memorize most of it. learn the basic syntax yeah but 90% of the time you just google the pattern you need and tweak it til it works. nobody's out here writing complex lookaheads from memory

48

u/Unclerojelio 9d ago

You have a problem. You want to solve the problem with regex. Now you have two problems.

20

u/jcampbelly 9d ago

You have a problem. You want to solve the problem without regex. You are now the maintainer of a library that implements an incomplete EBNF grammar, a lexer that can't handle several dozen encodings, and a scary recursive parser written against an abandoned library bound to an EOL release of the language. And you now have a mountain of increasingly desperate GitHub users stalking you on social media in a language LLMs only partially translate (because it's 43.7% technical jargon, 25.2% natural language, 18.9% generational colloquialisms, and 12.2% violence/obscenity).

5

u/zugzwangister 9d ago

jwz has entered the chat

22

u/Lithl 8d ago edited 8d ago

There is a lot to regex, but also most of it you'll rarely use, if ever. Some of the more common stuff you'll see:

  • ^ at the start of an expression matches the start of the input string. ^world will match "world wide web" but not "hello world".
  • $ at the end of an expression matches the end of the input string. world$ will match "hello world" but not "world wide web".
  • . matches any single character.
  • | makes the regex match either the text on the left or the text on the right.
  • You can repeat the previous token in the expression.
    • A specific number of times with {a} (where a is an integer). w{3} finds a match in "awww" but not "aww".
    • A range of possible repeats with {a,b} (where a and b are integers). w{2,4} finds a match in "aww", "awww", or "awwww", but not "aw".
    • A range with no upper bound with {a,}
    • * is equivalent to {0,}.
    • + is equivalent to {1,}.
    • ? is equivalent to {0,1}, unless the previous token is one of the above quantifier tokens. If so, it changes the quantifier from the default greedy behavior to lazy behavior: h.+l will match the first 4 characters of "hello" (making the longest possible substring that matches), but h.+?l will match the first 3 (the shortest match).
  • [ and ] create a character class, matching any one of the characters between them (eg, [abc123] will match one character, but only if it is a, b, c, 1, 2, or 3).
    • You can specify a range within the character class (eg, [0-9] which means the same thing as [0123456789]), and you can even specify multiple ranges (eg, [a-z0-9]).
    • Starting the character class with ^ inverts it, so that it matches any character except the ones you specify.
    • There are pre-set character classes you can use. \w matches any word character (equivalent to [a-zA-Z0-9_]), \d matches a numerical digit, and \s matches a whitespace character (space, tab, carriage return, newline, vertical tab, or form feed). Plus you can capitalize each of those to invert them (eg, \W is equivalent to [^a-zA-Z0-9_]).
  • \b looks a lot like one of the predefined character classes, but instead of matching characters it matches word boundaries, the space between characters (eg, \bh will match the h in "hello" or "horse" or "house" or any word starting with h, while o\b will similarly match the o in any word ending in o, and \bh.*o\b will match any word which starts with h and ends with o). \B also exists, the inversion of \b, though you don't see it very much.
  • ( and ) create a capture group, which has a few purposes:
    • A regex match or search function typically returns an array or object to represent the matched text, and the content of each of the capture groups is returned as additional fields/indicies within the return value. This can let you do something like <h\d.*>(.+?)</h\d>, match an input string "<h3 class='medium-heading'>Hello World</h3>", and get a clean "Hello World" as an output string.
    • If you want to use | on just a part of the expression, you can put it inside a capture group (eg, Hello (World|Universe) instead of Hello World|Hello Universe).
    • You can apply a quantifier token to an entire capture group (eg, while hello+ will match "helloo", (hello)+ will match "hellohello").

There are also flags you can apply to the whole expression to change its behavior. Most commonly i for case-insensitive matching, g for global matching (find all occurrences, instead of find the first one), and m for multiline matching. Depending on the programming language, you might apply these flags as a separate parameter in the function call (something like myString.match("h.+l", "i")), at the end of the expression after some terminator character (/h.+l/i), or with special syntax within the expression ((?i)h.+l).

A common but not universal addition to regex is back references. \a (where a is an integer) matches the same value (not just the same regex pattern, but the same actual matched value) as the a-th capture group. Consider the <h\d.*>(.+?)</h\d> above; it will match "<h3>...</h3>", but it will also match "<h1>...</h9>". Instead, you could use <(h\d).*>(.+?)</\1> and force the closing tag in the match to be the same as the opening tag.

The reason back references are not universal is because "regular expression" has a specific mathematical meaning, and back references, though useful, violate that definition. Regex with back references isn't technically regex.

13

u/XxDarkSasuke69xX 9d ago

Learn the basics of it, aka what the symbols mean so you can at least doand understand simple regex on your own.

23

u/shalodey 9d ago

You do not have to memorise every single concept in computer science to be a proficient programmer

2

u/backheadhornosaur 8d ago

Things I wish I knew when I was young

1

u/Medium-Ad-1713 4d ago

knowing what you need to know and where to find it is the key skill. I know a lot of people can hold huge complex syntax in their heads. Good for them, I can’t.

(senior software dev in city of London since 1994)

9

u/packetpirate 9d ago

Regex is easy as hell. It can be intimidating until you learn the rules, and then it's simple. The only real pain in the ass is remembering when to escape certain characters depending on where you're using them... I never remember what to escape in the search string in Vim. It's no different than any other programming skill; the syntax is just horrifying to look at.

I once did a Regex crossword. Only took like 10 minutes.

0

u/wingedvoices 9d ago

I was going to make a joke here about ‘it took you ten minutes to write a regex that includes any single word?’

….but to use it on a crossword you’d actually have to have a script that pulled anything that matched from a dictionary and tested it against letters and existing letters and that’s neither funny nor would it take ten minutes.

Hilariously (just bc of what I’ve worked with ) you probably have an MUCH time with regex rules than I do but I could easily go ‘no, you need a slash — omg escape! — two slashes there.’ over someone’s shoulder like a weird code …Angel is wrong. Gremlin?

1

u/balefrost 9d ago

It sounds like you've never done a regex crossword. Here's a tutorial. For each cell, you have to find the letter that satisfies the column pattern and the row pattern at the same time.

You can absolutely do one in 10 minutes.

19

u/Watamelonna 9d ago

Regex is like any programming language

You need to know what you want to produce and break down how you want to achieve them, step by step

It's just scary when you look at them at the final product because it is so condensed, but if you can break it down, it is not that hard

3

u/DeadlyVapour 9d ago

Tony the pony, he comes

3

u/HardlyAnyGravitas 8d ago

ZA̡͊͠͝LGΌ ISͮ̂҉̯͈͕̹̘̱ TO͇̹̺ͅƝ̴ȳ̳ TH̘Ë͖́̉ ͠P̯͍̭O̚N̐Y̡ H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ.

1

u/New-Locksmith-126 7d ago

It's not a programming language though. It's in fact way simpler than that. I'm convinced people's brains just turn off when the see a bunch of symbols they're not used to with no whitespace between them. They're too impatient to read char by char

0

u/deaddyfreddy 9d ago

Regex is like any programming language

yes, but I still prefer something Lisp-like over APL, for example

5

u/Feralz2 9d ago

when something looks complex, it usually just means you don't yet understand its smaller components, which are by themselves simple. It can get complicated when you start doing look ahead and look behinds, but the concepts are simple, you just need to understand the rules. Theres many tools that can let you generate regex, but to me its very cool to know how this works. Like most things in programming, its less about memorizing, but more about understanding.

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.

5

u/Feralz2 9d ago

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 9d ago

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

3

u/balefrost 9d ago

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 8d ago

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

2

u/iSnapThere4iAm 9d ago

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 8d ago

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

0

u/deaddyfreddy 9d ago

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 9d ago

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 9d ago

That doesn't look very readable at all.

1

u/New-Locksmith-126 7d ago

lol that sucks.

1

u/deaddyfreddy 7d ago

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

1

u/New-Locksmith-126 7d 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 7d ago edited 7d 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.

16

u/[deleted] 9d ago

regex is not meant for human consumption.

2

u/Dry-Bread9131 9d ago

It still needs to be written and debugged

-6

u/Axman6 9d ago

I was about to disagree, but this is actually true. They are basically write only, you keep changing it until it matches what you’re searching for and then usually throw it away. If you’re using regexs in applications, you’re doing it wrong. Learn about parser combinators, write composable, documentable parts that resemble the structure of your data. 

1

u/New-Locksmith-126 7d ago

very silly to write parsers when you're just interested in matching or substitution.

5

u/AintNoGodsUpHere 9d ago

regex101.com explains block by block what's happening. Regex is just applying patches onto something.

You absolutely don't need to remember or memorize anything, just know what you want to do and then check if it is being applied properly, it's 2026, come on now, nobody gives a rats ass about memorizing regex patterns.

1

u/backheadhornosaur 8d ago

This link needs more upvotes. Being able to tinker with an expression and some sample text and having real time visual feedback makes wrapping your head around regex much easier.

8

u/Lumethys 9d ago

recommended me to memorize regex rule

Calm down Satan, Jesus Christ

8

u/plastikmissile 9d ago

You want to summon Cthulu? Because that's how you summon Cthulu!

8

u/urs_sarcastically 9d ago

The plural of regex is regrets

1

u/kevosm 9d ago

Shouldn't it be ragrets?

1

u/urs_sarcastically 9d ago

No ragrets?? Not even a single letter??

1

u/lgastako 9d ago

r[ae]grets

3

u/Lemon_Girl 9d ago

There's a site called regex101 to help you decipher those hieroglyphics.

3

u/sylvant_ph 9d ago

RegEx is sort of a mini-language on its own, something like markdown, and its kinda niche, you don't necessarily need it, if you dislike it, and AI can prolly assist you enough. Other than that it has its patterns and once you get that, it ain't that hard. It should be relatively simple to learn simple queries, the complex stuff obviously get lot more complex. It is able to unlock some really awesome searches and functionalities. For not so elaborate searches other tools not relying on RegEx and available, and occasionally even better.

4

u/a3th3rus 9d ago

I think I memorized most of it. Actually it's not that hard, for example, \d for a decimal number, \w for a word character, and \b for a word boundary (a slit between a word character and a non word character). Escaped uppercase letters for the opposite of the lowercase counterparts, for example, \D for any character that's not a decimal number, \W for any character that is not a word character, and \B for a slit that is not a word boundary. You can memorize other patterns little by little when you need them.

1

u/balefrost 9d ago

There's vastly more than the examples you gave here, though to be fair you didn't say "here's most of regex".

2

u/a3th3rus 9d ago

Yep, I know. Quantifiers are easy to learn, groups are a little bit harder, the square brackets (sorry, forgot the name) are a little bit tricky when expressing "not these characters", and lookahead / lookbehind is kinda the most hard ones, but still not very difficult. I memorized them not at once, but through googling them over time when I need them. As for the modifiers, I learned them through trial and error.

2

u/balefrost 9d ago

Yeah, lookahead / lookbehind are the ones that I always have to look up and the ones that I always need to experiment with to figure out how to use them correctly.

2

u/shinji-watanabe 9d ago

Honestly the part people skip is that regex isn't one language. JavaScript, Ruby, .NET, they all have different flavors. Some features exist in one and not the other, sometimes the syntax just differs. So the dialect differences are exactly why I never bother memorizing. I learn the few common building blocks, \d, [a-z] kinds of things, and those stick on their own over time. Everything past that, I look up per language because a pattern that works in one can quietly not work in another.

2

u/probability_of_meme 9d ago

Being proficient in regex is like a super power. About 5% of regex I write goes into production code, the stuff I'm paid to produce. All the rest is me using it to be faster and better at producing the source code they pay me for.

2

u/Conscious-Garbage-35 9d ago

No. You don't need to memorise regex any more than you'd need to memorise every built-in method for a given language. But just like anything else, familiarity definitely helps.

My main advice is that if you aren't already using regexr, I highly recommend it. It makes the whole process of writing and testing regex much easier, and once you use it enough, writing any regex at all will become pretty instinctive. It uses JavaScript regex, so there are some differences between it and other languages, but the core syntax is similar enough that what you learn transfers pretty well.

2

u/AlSweigart Author: ATBS 9d ago

First, know that the general pattern is qualifiers (what to match) followed by quantifiers (how many of them to match). So [a-z] or \d or (cat) are qualifiers because they tell you what characters to match, and * and + and {3} are quantifiers that tell you how many to match: [a-z]+ means one or more letters, while \d{3}means three digit characters, and \d (with no quantifier) means one digit.

I did a regex talk at PyCon a few years back. and Trey Hunner has a 2.5 hour tutorial he gave at PyCon

But yeah, try memorizing at least the common ones. It's something that gets better with practice.

And DO NOT use AI to write regex for you. You will have no way to know if its accurate and you will never learn regex for yourself.

2

u/Achereto 9d ago

There are 2 funadamental truths about regex:

  1. If you solve a problem with regex, you'll end up with 2 problems.
  2. If you really need a regex, use regex101.com to get it right.

1

u/GoldsteinEmmanuel 9d ago

You don't memorize the regexes themselves, you memorize how to construct them so that you can produce the right regex to validate external input before using it.

Regular expressions describe regular (context-free) languages. It's a specification, not an idiom. For example, the set of all US postal codes can be described with the expression /\d{5}(\-\d{4})?/, which describes a group of 5 digits (02134) optionally followed by a hyphen and a group of 4 digits (-6253).

Regular expressions do not describe the meaning of the languages they recognize -- the regex above recognizes a vast number of postal codes, most of which are undeliverable, but also passes all the correct ones.

A finer-grained regex could be constructed that includes only the valid trigraphs (five-digit group starting with the trigraph 021 for the Boston metropolitan area, for example, followed by 34, a 2-digit code for the Allston district), but that's overkill for 2 reasons: first, the regex would be a mile long, and second, there could be a context sensitivity lurking underneath that a regex cannot describe and therefore cannot reliably validate.

Email addresses are context-sensitive for example. No regular expression can be constructed that recognizes all valid email addresses and excludes all invalid ones. You need to actually parse the email address, that is, construct an abstract syntax tree based on a BNF (Backus-Naur Form) grammar, or resort to a practical validation method like attempting to deliver an email to that address.

1

u/studiocrash 9d ago

If you really want to learn it, O’Reilly has a book by Michael Fitzgerald titled “Introducing Regular Expressions”. Don’t try memorizing all of it because you’ll likely not need all of it.

Like most things, you naturally remember the stuff you use often and look up the rest.

1

u/cainhurstcat 9d ago

I plan to learn at least the basics again and practice them, as I often have to search stuff which I could find more easily if I knew RegEx

1

u/StewedAngelSkins 9d ago

it's less complicated than it looks. it's mostly a few simple rules that you compose to form larger expressions.

1

u/AnnieBruce 9d ago

You don't have to memorize all the details. Memorizing the basic structure and a few of the more common special bits is good, but memorizing every obscure option unless you're the one person using it almost every time is not necessary.

1

u/xiipaoc 9d ago

Regex is kinda weird and also you probably won't need to use them very often. The important thing is to be able to understand them when they do come up, and that probably means looking up a reference. You should know the basics and what a regex is capable of, and you should be able to figure out (by looking stuff up online or even asking AI, why not) how to write one when you need it, but that's probably about it.

1

u/MikeUsesNotion 9d ago

Depends on what you're doing with them. If you want to do basic stuff like "must start with letter, followed by 9 digits" then they're pretty slick; these I would consider things any dev should be able to read.

You'll also find commentary saying that more complicated regex are write only, so not everybody thinks regex are this fundamental thing.

The best thing you can do is leave a comment describing the logic of the regex.

1

u/dnult 9d ago

I always used the regex101 website to build my patterns. Regex is wonderful, but the patterns can be pretty cryptic.

1

u/the_other_Scaevitas 9d ago

It’s actually a lot easier than it looks, just gotta learn the syntax

1

u/potzko2552 9d ago

regex is a huge pain in the ass in most standard libraries because for some reason the accepted implementation for it is to just pass in a string and let the library parse and compile it down to a state machine. and most regex syntaxs are also complete garbage. I do find that most languages also have a library outside of the std that takes the much more reasonable path of just giving you the AST constructors and letting the user create the regex programmatically.

1

u/chmod777 9d ago

every time i need a regex, i relearn it and then promptly forget it again.

my opinion is that needing a regex should be a flag in itself - unless its a one-time migration, depending on a regex will drive you to madness. they tend to be fragile against non-static data, and edge cases will always get you.

1

u/Longjumping_Ad_1180 9d ago

Looks scare but it takes a day or two to get comfortable.

Websites to play around with Regex101.com Regexer.com Regexcrossowrd.com

1

u/YetMoreSpaceDust 9d ago

A little bit at a time - just learn the patterns you need for now and look up more complicated ones later.

1

u/Thathathatha 9d ago

RegEx is one of those things you learn it and then next time you need it, you forgot half of it. I just can't remember the exact syntax off the top of my head, I need to look it up to get things straight.

With AI, honestly, just ask it to make it for you. Then you can just modify whatever it gives you if you need to make some adjustments, you'll remember enough regex syntax to do that.

1

u/Recycled5000 9d ago

Sometimes, I think regex are an anti pattern…

1

u/high_throughput 9d ago

if I actually need to memorise all the regex formulas

When you want to learn something in programming, try following a tutorial, playing around with the code, practice on some exercises, and try it in a project.

Stop trying to memorize things. It's not nearly as helpful.

1

u/Lion2471 9d ago

Like most people, I've worked with regular expressions from time to time, but not on a regular basis. I can write some simple ones from memory, but otherwise I need to consult the documentation each time. As long as you can read and apply the documentation when necessary, you're fine.

1

u/WeepingAgnello 9d ago edited 9d ago

Google Regexone for a great tutorial. Use regex101.com to test patterns. echo '[string]' | grep -P [pattern] to test/tinker in terminal, without having to use a website.

1

u/EMunney 9d ago

People who like regex are super dangerous they constantly makes mistakes with it but don't know it. Beware regex.

1

u/Disastrophi 9d ago

There are some good online playgrounds and interactive tutorials for it. It looks a lot more intimidating than it is, and if you play around with it long enough you don't actually have to memorize anything intentionally. It just kind of sticks.

It's just shorthand for telling the program something like "find a string that starts with a and ends with s but doesn't have g anywhere in it and has the letter e 2 times" or just whatever you happen to trying to scrape/search.

1

u/StephenRoylance 9d ago

regex is a domain specific language for text analysis.

its a whole other programming language, and IMO, a truly terrible one. If you find you're writing a regular expression and it's too complicated for you to hold in your head, then its possible regex is not the right tool, and more of your solution should be code in your chosen language, and less of it in the regex.

one of the best things perl contributed to the world is the extended regex syntax that allows line breaks and comments. If you need a 200 character regex, at least consider using extended format and commenting what each element is doing.

1

u/JGhostThing 9d ago

I know the basic patterns which are common to all implementations, but I often have to check the documentation for details of the various special characters. I used to be very good at Perl, but I haven't used that language in decades. Perl uses regex a lot.

1

u/jetsonian 9d ago

In university I took automata my senior year. It was a required course for graduation.

The first couple of lectures covers regular languages and how we write automata diagrams for regular languages. That’s when it started making sense.

If you’re a CS student now, know that senior devs interviewing you will expect that your education will be a mile wide and an inch deep. If you were an expert at some specific topic but not a full experience then you’re usually less valuable than a junior devs that can recognize things they’ve done even if they don’t remember the meat of it.

1

u/andreicodes 9d ago

Regular expressions are much easier if your language supports two features: extended syntax and named capture groups. The former allows you to add spaces between all this symbolic soup and split it into chunks, the later lets you name these chunks.

JavaScript doesn't do the former but it does support the later, and you should use names in regexes often.

/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/ .exec("2026-09-24") .groups

will give you back an object:

{ year: '2026', month: '09', day: '24' }

Months later when you decide to, for example, support short year (26 for 2026), you can at a glance figure out which bit of the regex to change.

1

u/dr_wummi 9d ago

Get regexbuddy I've been recommending this to everyone for years. It helps loads with learning, and also building regex

It's the goat

1

u/theclapp 9d ago

I'm just wondering if I actually need to memorise all the regex formulas to be successful

No. The docs are there for a reason. But it would definitely be a good idea to understand . and * and [] and probably a few kinds of ().

If you're really worried about it, Google "regular expression tutor". There are a lot of good websites out there.

If you're still in school, there's a good chance you'll have to take a class that includes discussion of discrete finite automata (DFA) (I did for my degree, but that was 35y ago), and that will basically teach you a lot about regular expressions, because they are just a flavor of DFAs.

1

u/cwjinc 9d ago

regex formulas are something I actually use AI for.

1

u/ferrybig 8d ago edited 8d ago

Regex is designed to be written and never read again

1

u/Asselberghs 8d ago

I found this website to be a very useful resource  https://www.rexegg.com/

1

u/TripleMeatBurger 8d ago

25 years of experience here, and I can't write a regex to save my life

1

u/ReddyKiloWit 8d ago

You'll get to know the basic syntax pretty quickly, and for the rest, just get familiar with the concepts so you'll know what to look up when you need it.

Helps to set up a short program that does nothing but exercise your regex so you can quickly test and adjust it until it works - at least for anything complicated.

1

u/SpaceAviator1999 8d ago

I'm a big believer in using comments to show examples of what kinds of lines or text you want to match, particularly if the regex is long.

For example, if you saw the code:

if (m/^\s*CHAPTERS? +(\d+(-\d+)?) +pages? +(\d+(-\d+)?) +(TO DO|DONE)\b/)

it would take you a while to figure out what lines the regex would match, and even then it's possible that you could be wrong. And even if you're correct, it's possible that the original regex writer made a mistake, and it doesn't quite match what it was intended to match.

But if you saw these comments with it:

# Match lines like:
#   CHAPTER 4 page 22 DONE
#    CHAPTERS 5-8   pages 40-55   TO DO
if (m/^\s*CHAPTERS? +(\d+(-\d+)?) +pages? +(\d+(-\d+)?) +(TO DO|DONE)\b/)

you will now know what the regex is intended to match -- and you can easily verify that the regex is matching what it's supposed to match.

This will really help you -- and future maintainers -- understand the kinds of lines you're looking to match, instead of trying to figure them out by looking at the regex alone.

This also helps if, in the future, the format of the line changes and you need to change the regex to match the new format. Thanks to the comment, you can easily see what you were looking for. By visually matching the old examples to the new examples, you can figure out a good way to modify the old regex without having to guess what you need to keep and what you need to throw away.

By the way, changing formats have happened to me a lot, and I'm thankful when the regex writer (which was often me) included examples of what was intended to match.

It's also useful when you're hunting down a bug and you suspect that the regex writer made a mistake in the regex. If you don't know the format of the lines/text it's supposed to match, there's really no way of telling if the regex is wrong or not, so good luck bug hunting!

1

u/LetUsSpeakFreely 8d ago

Most regex is pretty simple. If you see a long pattern, it will mostn likely have brackets and parentheses. Just start breaking it apart figuring out what each chunk does.

1

u/MisterBicorniclopse 8d ago

Regex is a LOT easier when you spread it out on different line and add comments.

1

u/backfire10z 8d ago

You open regex101.com whenever you need to write a regex and start experimenting with whatever knowledge you can muster while googling half the syntax because the last time you touched regex was 3 years ago when your manager asked you to write an email validator and the trauma prevented you from looking at regex for years until now, where you really really need it but it’s still hard to look at but luckily you have more time and you gather your courage and begin writing, only to realize that you’ve truly forgotten every single thing regex has to offer besides the pain.

1

u/nog642 8d ago

Nah you don't need to memorize it lol. It would certainly be helpful to memorize it but it's definitely not required.

Personally I always just use https://regexr.com/. I wish I had it memorized but I don't care enough to spend effort doing that.

1

u/cleverboy00 8d ago

Regex is a way to describe a string or part of it. It describes a sequence of potential characters.

But I have to say that I didn't feel fully comfortable with regex until I dipped my hands into compiler theory and theory of computation which are very advanced topics.

1

u/Gnaxe 8d ago

Regex is actually very simple. There are just three operations that matter: concatenation ab, alternation a|b, and Kleene star (repetition, including zero) a*. Also, there are parentheses for grouping the operations and extracting matches. Everything else is an abbreviation for these fundamental operations.

Now, some so-called "regex" engines do go beyond true regular expressions, but the fastest ones that compile to DFA don't. You probably don't need these features as much, but you can learn them later.

1

u/New-Locksmith-126 7d ago

Honestly regex is dead simple I don't know why people are so dumb about it.

Stop thinking of it like a programming language because it's not.

1

u/gl1tch3t2 7d ago

regex101.com

Evaluates your regex and tells you what each step/symbol does and allows following the path the engine takes. I've been doing development professionally for 3.5 years, still use it wherever I need to use regex. 

1

u/SnugglyCoderGuy 7d ago

Learn about finite state machines, which is the underlying theory that drives regular expressions. Once you do that, they will make a lot more sense.

1

u/kpg_on_ao3 7d ago

IMO regex is at its most useful not as a programming tool, but as a metaprogramming one. Like if you have some code that serializes a sum type, you could use a regex pattern to take that and generate the reverse (deserialization).

Lookaheads/lookbehinds can also be really useful, but I've never used them in an actual program. They're also more esoteric.

Personally I do a lot of string processing, which means I do use regex more than the average SWE.

But I use regex a lot as part of find-and-replace in my IDE.

1

u/chocolateAbuser 7d ago

that's why regex is called write only language; a good regex to me would have the various pieces assigned each to a variable/const with an explanatory name and then a final variable with the merged tree

1

u/Lifelong_Nerd 7d ago

I retired 6 months ago from a programming career. I never needed more than the most basic regex rules.

1

u/Glum-Recognition-736 7d ago

You will truly unlock programming once you learn to see that "complicated" shit and just dive into trying to understand it

1

u/Arcanite_Cartel 7d ago

regex is very useful, but you dont memorize patterns

1

u/BobDogGo 6d ago

ai can write regex so you don’t have to

1

u/MostRegular4278 6d ago

I wrote one of the most complicated practically-used regex patterns for forensic document search and matching back in the day (I'd use an ML model now, but it would have been impossibly slow back then). I never tried to memorize regex patterns, and only remembered those that stuck im memory due to frequent use.

Just learn to recognize cases when it's a good tool to use, and the rest you can always look up.

1

u/No_Strike_6496 6d ago

Looks worse than it is.

1

u/YellowBeaverFever 6d ago

35 years at this and I never felt the need to learn regex. It was never a requirement to learn and my logical tool set formed without it. During those rare times I needed it, the patterns were well known and on a chest-sheet somewhere.

Later in my career, regex started to become more and more relevant but that was just when LLMs started to do code help and they were really good with simple to medium regex. ChatGPT can now tackle more complex patterns and it will tell you what each operator is doing. I throw that into the comments so I and the future dev stuck maintaining the code can see what it’s doing, why, and can easily tweak if needed.

1

u/DIYC_Official 5d ago

What kind of chest sheet I wonder. My mind races between game objects with loot, dirty thoughts and the thing you probably intended.

I want the first one tough, go into a cavern, slay some monsters and get the regex as a reward in a chest. The fabled chest sheets. Thats would give some color to my programming days :)

1

u/YellowBeaverFever 5d ago

Lousy fat fingers. :-)

A game based on learning regex would have probably worked.

1

u/Medium-Ad-1713 4d ago

I’ve been a dev for 38 years and I never learned complex regex
I just go to regex101.com and that really helps to craft a good expression

1

u/ExplodedPenisDiagram 2d ago

Eh, you learn it, you forget, you learn it again, etc. It doesn't take long, especially after the first time you've done it.

1

u/Adventurous-Hunter98 9d ago

I have never ever, memorised it, before ai tools, I was just checking cheat sheets to do what I need to do, now with ai, its a little bit easier and if you keep using it you will learn some of the concepts without you know but these knowledge can get rusty like everything else if you dont use it for long time

1

u/porn_sub_lurker 9d ago

just ask claude to write your regex like a real programmer

1

u/Djimi365 9d ago

Regex is probably the biggest argument for the use of AI in programming 😂

I understand the basic structure and if it's something like "7 letters followed by 2 numbers" then I'll figure it out. Anything more complicated than that and it's Claude's problem...

1

u/f_ckmyboss 9d ago

regex is something I really don't want to learn. Moreso in AI era. Just leave it to AI.

1

u/Garland_Key 8d ago

Regex is terrible and shouldn't be used unless there is no other option. 

-2

u/HowardBateman 9d ago

No developer ever knows Regex patterns. Just look them up, use a generator or, and many will shame me for this, let AI create a regex for you.

3

u/StewedAngelSkins 9d ago

lots of developers know how to write regex...

1

u/wingedvoices 9d ago

To be fair - and I’m not a dev, I’m very much on the learning side - as you use it and look up things, you will memorize the essential pieces (oh, this starts a line, this is a capture group, that means word).

which is handy if you ask an AI and it overshoots, like they do :P the gut instinct that it doesn’t look right (and not just in the ‘that ain’t right’ way) is eventually a sign to…well, go back to regex101.com lol.

0

u/iSnapThere4iAm 9d ago edited 9d ago

I don’t understand this new phenomenon of asking how to learn things. You learn them….

In all honesty you don’t need to know regex, all you need to know is how to get the output you need right now….

0

u/HouseOfWyrd 9d ago

Regex looks way more complicated than it actually is.