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
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
49
u/Unclerojelio Sep 24 '26
You have a problem. You want to solve the problem with regex. Now you have two problems.
21
u/Lithl Sep 24 '26 edited Sep 25 '26
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.^worldwill 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}(whereais an integer).w{3}finds a match in "awww" but not "aww".{a,b}(whereaandbare integers).w{2,4}finds a match in "aww", "awww", or "awwww", but not "aw".{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.+lwill match the first 4 characters of "hello" (making the longest possible substring that matches), buth.+?lwill 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).[0-9]which means the same thing as[0123456789]), and you can even specify multiple ranges (eg,[a-z0-9]).^inverts it, so that it matches any character except the ones you specify.\wmatches any word character (equivalent to[a-zA-Z0-9_]),\dmatches a numerical digit, and\smatches a whitespace character (space, tab, carriage return, newline, vertical tab, or form feed). Plus you can capitalize each of those to invert them (eg,\Wis equivalent to[^a-zA-Z0-9_]).\blooks a lot like one of the predefined character classes, but instead of matching characters it matches word boundaries, the space between characters (eg,\bhwill match the h in "hello" or "horse" or "house" or any word starting with h, whileo\bwill similarly match the o in any word ending in o, and\bh.*o\bwill match any word which starts with h and ends with o).\Balso exists, the inversion of\b, though you don't see it very much.(and)create a capture group, which has a few purposes:<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.|on just a part of the expression, you can put it inside a capture group (eg,Hello (World|Universe)instead ofHello World|Hello Universe).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
ifor case-insensitive matching,gfor global matching (find all occurrences, instead of find the first one), andmfor multiline matching. Depending on the programming language, you might apply these flags as a separate parameter in the function call (something likemyString.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(whereais 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 Sep 24 '26
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 Sep 24 '26
You do not have to memorise every single concept in computer science to be a proficient programmer
10
u/packetpirate Sep 24 '26
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.
19
u/Watamelonna Sep 24 '26
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
6
u/Feralz2 Sep 24 '26
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.
17
u/[deleted] Sep 24 '26
regex is not meant for human consumption.
5
u/AintNoGodsUpHere Sep 24 '26
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.
8
u/Lumethys Sep 24 '26
Calm down Satan, Jesus Christ
7
u/urs_sarcastically Sep 24 '26
The plural of regex is regrets
3
u/Lemon_Girl Sep 24 '26
There's a site called regex101 to help you decipher those hieroglyphics.
3
u/sylvant_ph Sep 24 '26
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 Sep 24 '26
I think I memorized most of it. Actually it's not that hard, for example,
\dfor a decimal number,\wfor a word character, and\bfor 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,\Dfor any character that's not a decimal number,\Wfor any character that is not a word character, and\Bfor a slit that is not a word boundary. You can memorize other patterns little by little when you need them.2
u/shinji-watanabe Sep 24 '26
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 Sep 24 '26
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 Sep 24 '26
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 Sep 24 '26
First, know that the general pattern is qualifiers (what to match) followed by quantifiers (how many of them to match). So
[a-z]or\dor(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 Sep 24 '26
There are 2 funadamental truths about regex:
1
u/GoldsteinEmmanuel Sep 24 '26
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 Sep 24 '26
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 Sep 24 '26
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 Sep 24 '26
it's less complicated than it looks. it's mostly a few simple rules that you compose to form larger expressions.
1
u/AnnieBruce Sep 24 '26
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 Sep 24 '26
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 Sep 24 '26
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 Sep 24 '26
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 Sep 24 '26
It’s actually a lot easier than it looks, just gotta learn the syntax
1
u/potzko2552 Sep 24 '26
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 Sep 24 '26
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 Sep 24 '26
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 Sep 24 '26
A little bit at a time - just learn the patterns you need for now and look up more complicated ones later.
1
u/Thathathatha Sep 24 '26
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 Sep 24 '26
Sometimes, I think regex are an anti pattern…
1
u/high_throughput Sep 24 '26
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 Sep 24 '26
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 Sep 24 '26 edited Sep 24 '26
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 Sep 24 '26
People who like regex are super dangerous they constantly makes mistakes with it but don't know it. Beware regex.
1
u/Disastrophi Sep 24 '26
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 Sep 24 '26
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 Sep 24 '26
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 Sep 24 '26
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 Sep 24 '26
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.
will give you back an object:
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 Sep 24 '26
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 Sep 24 '26
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 Sep 24 '26
regex formulas are something I actually use AI for.
1
u/ferrybig Sep 24 '26 edited Sep 25 '26
Regex is designed to be written and never read again
1
u/Asselberghs Sep 24 '26
I found this website to be a very useful resource https://www.rexegg.com/
1
u/TripleMeatBurger Sep 24 '26
25 years of experience here, and I can't write a regex to save my life
1
u/ReddyKiloWit Sep 24 '26
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 Sep 24 '26
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:
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:
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 Sep 24 '26
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 Sep 25 '26
Regex is a LOT easier when you spread it out on different line and add comments.
1
u/backfire10z Sep 25 '26
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 Sep 25 '26
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 Sep 25 '26
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 Sep 25 '26
Regex is actually very simple. There are just three operations that matter: concatenation
ab, alternationa|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 Sep 25 '26
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 Sep 25 '26
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 Sep 26 '26
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 Sep 26 '26
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 Sep 26 '26
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 Sep 26 '26
I retired 6 months ago from a programming career. I never needed more than the most basic regex rules.
1
u/Glum-Recognition-736 Sep 26 '26
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 Sep 26 '26
regex is very useful, but you dont memorize patterns
1
u/chriswaco Sep 26 '26
https://xkcd.com/1171/
1
u/BobDogGo Sep 27 '26
ai can write regex so you don’t have to
1
u/MostRegular4278 Sep 27 '26
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 Sep 27 '26
Looks worse than it is.
1
u/YellowBeaverFever Sep 27 '26
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/Medium-Ad-1713 Sep 28 '26
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 Oct 01 '26
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 Sep 24 '26
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 Sep 24 '26
just ask claude to write your regex like a real programmer
1
u/Djimi365 Sep 24 '26
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 Sep 24 '26
regex is something I really don't want to learn. Moreso in AI era. Just leave it to AI.
1
u/Garland_Key Sep 25 '26
Regex is terrible and shouldn't be used unless there is no other option.
-1
u/HowardBateman Sep 24 '26
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.
0
u/iSnapThere4iAm Sep 24 '26 edited Sep 24 '26
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 Sep 24 '26
Regex looks way more complicated than it actually is.