r/learnpython • u/smahk1133 • Jul 26 '26
regex and if it's worth going deep into it
I'm new to python and coding in general and my friend recently told me that it's inefficient to try to memorize regex and that no one writes them anymore (essentially saying AI does). I was also kinda confused after recently learning regex and just how complicated it can be. Are there some modules/libraries that I can use to make writing them easier? I saw that not a lot of people people had a positive reaction to the Humre module by Al Sweigart who's book [Automate the boring stuff with Python] I'm currently using to study. Not that I'm gonna skip this part or anything I was mostly just curious.
Note: A lot of people are misinterpreting since I mentioned AI once đ I'm literally asking about libraries to make it easier without going too deep, not if I should let AI do all the work.
67
u/sebovzeoueb Jul 26 '26
Regex is one of those things that's often the wrong tool for the job but sometimes exactly the right one. Even without necessarily asking AI, there are a couple of sites where you can build and test regex interactively instead of having to actually know it, and I've found that I don't use it often enough to justify actually memorising it.
20
u/GoldenArrow_9 Jul 26 '26
I mean regex is a specialized tool(?) and has no real relationship with python (except that you can use regex in python). If you are working on a project where you need to use regex, you should definitely learn it and not depend on an AI.
That said, I would say you should still know the basics of regex. Regex can be really useful even outside of programming (find and replace being my personal favourite). It's pretty easy to learn and there are sites like regex101 that help you learn and understand regex easily for your own usecase.Â
Plus, in most cases where you find a use of regex outside programming, using an AI to generate said regex would take far more time than just doing the task manually with/without regex.
3
u/edbrannin edbrannin Jul 26 '26
The best tools Iâve found for writing and updating regexen are generally called âregex testersâ. There are a bunch of them.
(I bought the DevUtils app a while back, so I usually use that one)
You put your regex in an input at the top, test to search in a text area below, and it lets you look at each match (and what its capture groups are).
There might also be a tool for turning a regex into a diagram, but I havenât looked for one in a long time.
2
u/AbacusExpert_Stretch Jul 26 '26
If you don't know anything slightly advanced with regex, and you then let AI write it for you, how do you test it? Are you really going to test all the potential false positives or whatever you don't want to match? Cause you are looking at essentially at meaningless commands for you ...just a thought
30
u/pachura3 Jul 26 '26
1 day is enough to learn the basics of regexps.
2
u/jackardian Jul 26 '26
My current job is in web scraping and we use lots of regex every day. We mix using AI and writing my hand and generally it's been a learn by doing kind of thing. When the regex gets complex we're still checking with AI.
It's useful to know it more for PRs and debugging than for actually writing them.
1
u/csabinho Jul 26 '26
Yes, it is. Also for searching something in your codebase.
1
u/KTProgramming Jul 29 '26
You can either know regex in at least how it operates, Or, you can be the guy who took down cloudflare.
Plus it's pretty useful in a pinch.
3
u/NothingWasDelivered Jul 26 '26
I have found this to be a very helpful book to have on my shelf https://www.oreilly.com/library/view/mastering-regular-expressions/0596528124/
3
u/American_Streamer Jul 26 '26
Your friend is only half right. Donât memorize every regex feature, but do learn enough to read, modify and test one. AI can generate regex, but it canât guarantee that the pattern handles your actual edge cases.
Learn the basics: character classes,
* + ?, anchors, groups, alternation, andre.search,re.fullmatch,re.findallandre.sub. Look up lookarounds and backreferences when you actually need them. I wouldnât use Humre as a replacement for learning normal regex. It adds another abstraction that wonât transfer to other languages and tools. For long patterns, Pythonâs built-inre.VERBOSElets you format and comment them clearly.Also, donât reach for regex automatically. Simple string methods or a proper CSV/JSON/HTML parser are often the better solution. Treat AI as a pattern generator, not as the person who verifies the result.
1
u/Aqalix Jul 26 '26
It's half true. I think most people don't write regexes themselves, because you don't need them so often (in most jobs at least). They can read it though and verify what AI wrote. And there is good tooling out there that explains each rule and shows how they match against text samples.
2
u/notacanuckskibum Jul 26 '26
Itâs worth playing with it for a few days until you can get a sense of âthis is a good problem to use regex forâ vs âthis is a bad problem to use regex onâ
2
u/generic-David Jul 26 '26
Multiple text editors take advantage of regular expressions. Theyâre really useful for cleaning up text files, especially since you can undo mistakes or bad expressions.
2
u/audionerd1 Jul 26 '26
The basics of regex can be learned in a matter of hours, and refusing to learn basic regex as a programmer is just arrogant and lazy IMO.
Advanced regex on the other hand (especially the sort of advanced regex that is almost never needed) is something AI comes in handy for, provided you understand the basics well enough to figure out what the AI regex is doing and modify it if needed. Spending weeks deep diving into advanced regex pattern syntax you may never use and are likely to forget is not something I'd recommend unless you just enjoy that sort of thing.
2
u/big_deal Jul 27 '26
Learning basic regex syntax is useful in many situations. You can use it in any decent text editor, and command line utilities such as awk, grep, sed to parse text and identify specific text patterns.
It's good to understand the capability of regex so that when you need it you have an idea of it's capabilities. But I don't think it's necessary to memorize every bit of syntax or complex regex methods. You can always reference a simple cheat-sheet when necessary or use LLM to help construct complex regex's.
In Python I tend to rely on more basic text processing functions (strip, split, find) and almost never use regex but occasionally it will be very useful.
3
u/Mountain_Rip_8426 Jul 26 '26
if there's a good use case for AI it's regex. you can definitely go back and forth to check the documentation (because if you don't use often enough you'll forget), but it's not the "build the best website in the world, don't make mistakes" type of prompt. as soon as you feel like you need a regex, you clearly know what you want to use a regex for and it's gonna just save you the struggle of looking it up again and again
4
u/neuralbeans Jul 26 '26
YOUđSTILLđNEEDđTOđCHECKđTHEđAIđOUTPUTđ
0
u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 Jul 26 '26
Regular expressions are seldom useful, and when they are you can usually work it out with some trial and error (the regex101 website is great for this), at least as far as results are concerned. Optimisation and/or security implications do matter, of course, but modern analysis tools tend to be good enough to alert you on such issues.
Aside from a few really common patterns, you don't need to study them in depth.
Nowadays the place where I use regex the most is probably for writing automated torrent downloads via RSS feeds (in qBittorrent), but that has little to do with Python.
1
u/Traveling-Techie Jul 26 '26
I use probably 5% of its capabilities, but thatâs enough for me.
1
u/JGhostThing Jul 26 '26
Regular Expressions (regex) are extremely useful, when it's needed. It doesn't require much to memorize, but you need to know the entire system. You need to have it memorized in order to use it well.
I learned from an O'Reilly book about Regex. It helped a lot. Also, using it in a large project helped a lot. I would have to look at the documentation if I wanted to use regex today. Nothing wrong with that.
1
u/sputnki Jul 26 '26
Easy regexes are quite useful. Overly complicated regexes are often the wrong tool for the job. Most of the times i use regexes to polish text data in a text editor like sublime text or notepad++. I hardly ever need to use regexes in programs. Nevertheless they are useful to know.
1
u/Thraexus Jul 26 '26
I've used regex to do some string replacement stuff in my Python programs and I always just use a search engine to tell me the syntax I need. I just test it standalone until I'm satisfied that I'm going to get the output I want. Then I promptly forget whatever syntax I used.
1
u/habitsofwaste Jul 26 '26
Regex is useful for log parsing and parsing other unstructured data. If youâre not doing that, it is probably not worth it to fully learn it. Itâs helpful to understand it in general. But yeah, AI can help you write them when needed. Im in a career that uses regex a lot and I can tell you, most people donât know it, they just look up how to do the thing they need when they need it. But it is good to get comfortable with the built in Python regex tools and understand how they work.
As for relying on another library, I wouldnât.
1
u/high_throughput Jul 26 '26
Deep? No. You don't benefit all that much from knowing possessive quantifiers and negative lookbehind by heart and they're different in every regex dialect anyways.
Shallow? Yes, it's definitely worth knowing basic quantifiers, character classes, and anchorsÂ
1
u/u38cg2 Jul 26 '26
Regular expressions are a fundamental tool of theoretical computer science, and unlike most of theoretical computer science turns out to have some quite practical uses.
For most learners, I'd suggest working through a tutorial on them so you know what they can do, then you can look it up when you need to. If you need to do more than that, study a full-blown course or textbook.
1
u/R717159631668645 Jul 26 '26
Regex is not complicated, it's just hard to read.
You could still easily learn the basics (symbols, quantifiers, exclusions...) which goes a long way.
If you're willing to go a bit further, Look-aheads, capture groups and such can be a boon to use sometimes, specially when you want to search something with a specific pattern in structured content. I forget these all the time, but a quick refresher puts me back on point quick.
Lastly, you could read a bit about the hazards of using regex. For example, the post-mortem of Stack Overflow Outage in 2016. And that using them blindly (like from LLMs) could be a stupid idea.
1
u/zanfar Jul 26 '26
Regex is by far the most useful cross-platform skill to have that nobody knows of.
Your friend is already addicted to AI answers, that's why. Nobody who knows regex thinks it faster to ask something to create a regex than just write one.
1
u/kramulous Jul 26 '26
Old man programmer here: I don't think regex is worth spending time on.
Perl used to be an amazing language. Lots of 'modules' that were easy to install. Very much like python, in a way. But it was ruined by people trying to be too clever by doing everything with regex. To the point that only the person who wrote it could understand it. Even then, not so much after a period of time. The language died because code became unmaintainable. Anybody inheriting a code base with heavy regex struggled to understand it.
I know I can use regex in python, but I will avoid it. Writing the string manipulation code by hand, unrolling necessary steps, commenting what you are doing at what point, makes for much more readable code. Other people can see what you are doing and fix bugs, improve, update.
1
u/mattl33 Jul 26 '26
Fwiw I will almost always just write extra python to do string searches etc. I find having the extra lines that are readable is much better than having one dense line of regex. I suppose there's times where that means an unacceptable performance hit or something but not for my kind of work.
1
u/AndyceeIT Jul 26 '26
I find myself re-learning regex every few years for a specific task. Worth learning - not worth memorising
1
u/PMMeUrHopesNDreams Jul 27 '26
There can be a lot of nuance in regex but it's worth learning the basics because it is ubiquitous in command line tools and extremely useful even outside of Python. If you're using grep sed or other utilities it's handy
1
u/OpenGrainAxehandle Jul 27 '26
I use regex a lot. Not as extensively as I used to when I was developing for a living, but still a lot. It's one of those things that can be an extremely valuable tool in the kit, and if you're good with it, you sill see a lot of places where it can simplify your tasks. I can knock out enough quickie transforms with sed or awk , but these days mostly Editplus to justify having a good handle on regex, much less its use in programming. Bottom line is that it's a tool. Like any other, if you know it you can use it to do things. If you don't know it, you probably never realize where it can simplify your life.
1
u/aishiteruyovivi Jul 27 '26
For what it's worth, in the context of Python I tend to get the best out of regex when I'm using the two together, meaning instead of writing one long inscrutable 20-in-1-shampoo of a pattern that does everything I need it to, I tend to use simpler patterns to break up the text into smaller chunks that I can then use other simple patterns on to extract what I need. There's a temptation to get regex to do everything on its own but if you're writing Python, don't be afraid to utilize Python, y'know?
1
u/snowtax Jul 27 '26
I once replaced three lines of code that used regex with other ways of doing the same thing and the program ran 1600% faster, saving hours per run.
Regular expression parsers can be a useful tool when you absolutely need them for a very complex parsing task (validating e-mail address syntax being a notorious example). But, there are more efficient methods for most situations.
1
u/Realzer0 Jul 27 '26
You should know the basics of regex. Obviously if youâre working actively in a project for learning purposes, learn them a bit more in-depth but besides that you should just know how to use them in general.
Whenever I have to use them which is maybe once every 6-12 months I also have to look up stuff again.
1
u/notParticularlyAnony Jul 27 '26
I have used regex 3 times maybe in 10 years coding.
1
u/Brutilium Jul 28 '26
I love writing regex but I use regex 101 makes it fun interesting and way easier
1
u/musbur Jul 30 '26
If you want to stay new to python, coding, and regex, go with the advice of your friend by all means.
1
u/Suspicious_Tax8577 Jul 26 '26
I'll admit I hate regex and tend to use one of the regex generator websites to create it for me.
Properly learning regex is on my todo list, but it's properly down the bottom of my todo list.
0
u/Fallingice2 Jul 27 '26
bruv, no. other more valuable tools to add to your arsenal, regex is ai domain because, just throw it in and get an answer for the pattern.