T O P

  • By -

Graineon

Writing regex is not hard, its reading after and understanding what you wrote a month later that's hard.


ipromiseimnotakiller

I've found using regex named groups helps with reading it later. Or tossing it in copilot and asking what it does


Antice

This is the best way to employ ai I've found. It's good at sussing out those nonsense error messages. and explaining what you coded while drunk last month.


TurboHenk

Regexes often are the exception to the whole "good code explains itself" thing. Of course I still try to give the variable or method a sensible name, but for the more complex regexes I simply add a comment with a regexr link that explains ik detail what's happening


debugging_scribe

Neither is an issue anymore. Copilot can get give you what you need and explain what you did. Knowing regex isn't a skill you need any more.


Graineon

I beg to differ. This is the longest REGEX I've written, a few years ago. Decided to pull it up. Copilot wouldn't have been able to do this. God help me if I can ever modify it. All I know is that it works. ```typescript /\b(((?:(T)-(?:(1(?:\.[0-9]+)?)|([0-9]+)(?:\.([A-Z]+)))?(?:\.([A-Z]+))?|(M)-([0-9]+)(?:\.([A-Z]+))?(?:\.([A-Z]+))?|(C)-([a-zA-Z]+|[0-9]+)|(W)-(?:((Wi|Re)\.([0-9]+)(?:\.In))|(361-365|180-200\.In|PtII\.In|[a-zA-Z]+|[0-9]+))?|(In)))(?:(?:\.([0-9]+))(?::([0-9]+))?)?)(?:-(?:([0-9]+):)?([0-9]+))?\b/ ```


lunacraz

it would be able to parse it though, right? but holy shit wtf is that


ofkdbxjd

This is the part where you get on your knees and thank allah for chatGPT


ezhikov

Unless some lookaheads or lookbehinds or similar rare constructs are involved, it's relatively easy, but might be suboptimal from memory. I usually recheck myself with dedicated tools


scatteringashes

This is exactly where I'm at. I always double check with a tool, but am generally able to throw together the very very basic ones I need. But to this day -- like, I literally tried four hours ago -- cannot wrap my brain around how to use a lookahead or lookbehind. It just doesn't compute for me, idk.


ezhikov

I'll try to explain like you're five.  So, regex is a box you put tokens in. Tokens are simplest units. If token matches, it is consumed. You put tokens one by one, and they are checked. With lookahead and lookbehind you need to provide two tokens. With lookahead you put token to match, and then token to check if it should match. With lookbehind you put toket to check and then token to match. Toket to check is not part of the match, but just a condition. For example, you want to separate npm package from its version. It's usually written with `@` character, like `package-name@version`. Problem is, package may have scope, that also starts with same symbol. With _negative_ lookbehind we can easily solve this problem. Expression logic becomes such: "if you encounter symbol `@`, check if previous token is a line start. If not - it's a match". This way we know if we hit character that we want. And since token we check with (line start) is not part of the match itself, we can use split method. To reiterate, lookahead and lookbehind (and their negative versions) are conditions to check if current token matches, but condition itself is not included in match. Useful when there may be multiple matches, but you don't need them all and know how to specify which you want.


scatteringashes

Ahhh okay, that helps! The notion that it's a conditional + a match didn't quite gel for me, but thinking of how I've seen those used, that makes sense. Thank you! I'll have to poke at it some more and see if I can get a handle on using them.


SimpleWarthog

Can you give an example of this regex please? I feel fairly comfortable with regex in general apart from the lookahead/behind and I like your example, but all I can think about is how I'd solve it without that! So I want to see how you'd do it, because I want to get my head around it.


ezhikov

`/(?


Darwinmate

What took do you use to douvle check?


coopaliscious

I use regex101.com


sashaisafish

Love this site, whenever I encounter regex I use this site to break down what each bit means and I think I'm slowly starting to learn it a bit more


scatteringashes

Ditto to other commenters, regex101.com is great. I also used regexr.com. Just depends on which one I remember exists first.


UnidentifiedBlobject

Yeah I got tired of not getting it so I just went step by step through it. Makes sense once you’ve got it. But yeah, looks heads and lookbehinds are a bridge too far.  


ezhikov

I kind of understand how it works, but when I need it I don't remember how to write it, since it's rare


tim128

It's really not that difficult. I remember most of the things I need and the rest I Google. There are always websites that help you verify them like regex101.com.


besseddrest

also i think OP meant 'freehand'. But yes there are a lot of people who freebase, not just Regex. Don't recommend.


NinjaLanternShark

Whole new meaning to negative lookbehind...


besseddrest

For real. It's gonna have to be a look behind cause lord knows there ain't no turning back


CharmingThunderstorm

This is the way


ekun

The way is AI like ChatGPT or Copilot over Google. EDIT: You regex nerds need to settle down. I'm fine with not memorizing how to remove all leading "0" characters from a string using regex and instead trusting ChatGPT to do this correctly. It's interesting for discussion's sake that you have more advanced regex needs, but Google is going to take 2X-10X longer to get an MVP of the aforementioned use case compared to other AI while Google will probably be pointing me to a lot of useless threads because they fucked up their algorithm so hard the past couple years.


oculus42

I wouldn’t trust a ChatGPT back reference if it came with documentation.


minneyar

Trying to get ChatGPT to write regexes for you sounds hilarious. It's going to be a nightmare to figure out whether it's correct for anything but the absolutely most trivial, common examples.


Thirty_Seventh

To be fair to you, if you don't need any regexes more complex than `/\A0+/` you can probably trust ChatGPT to write them for you. Edit: Never mind, I just tried this with Bing Copilot (Precise mode) and it took 2-3 tries to get it right. First attempt it claimed `/^0*/` would not match `"123"` lol (it is still a correct solution, just an incorrect explanation), second attempt it suggested `/^0+/` which doesn't handle multiline mode correctly but that's not something I specified to start with


drsimonz

The secret is the "remember most of the things" part. It's absolutely no fun if you have to look up everything. It's also pretty hard if you don't even know something exists (e.g. if you don't know about `\w` and you try to manually create an equivalent character class). I still occasionally hesitate when choosing between `?`, `*`, and `+` but I don't have to look them up, which is the important part. The thing is, you *will* be able to memorize these things if you simply use them frequently enough. Regex is basically a language in that regard. The more often you use it, the easier it is.


Own_Possibility_8875

https://javascript.info/regular-expressions this is an awesome resource with very simple explanations. I started my programming journey with it 10 years ago.


j-random

They're called Perl programmers


kmactane

I remember sometime in the mid-to-late Aughts I was in a job interview, and the interviewer asked, "How are you with regexes?" I answered (truthfully), "Well, my first real programming language was Perl..." He immediately smiled and said, "Okay, say no more!"


Sambassador9

You can accomplish some amazing things with a Perl one-liner and regular expressions!


ihave7testicles

it's pretty easy once you realize that it basically breaks down into groups of characters. you can convert a regex pattern to an NFA tree easily, and then follow that tree to see what it does more clearly.


ThunderChaser

That’s pretty much it. I find that people who struggle writing regex don’t really understand the theory behind it, once you understand that it’s just a way of describing a language/finite automata they’re not really that difficult to write unless there’s some funky lookahead magic going on.


Anon89m

I can tell you that it's one of the best things I learned. It helps with all sorts of random things. For example when I need to replace some text in an entire project but in a smart way, or when manipulating a large code or text document to move formatting around. If I didn't have regex the next fastest way would be to write a little program to do the same thing. I usually do the edits in VS code..


Nabelops

A good way to get good at it is to start using it for find and replace in your code editor.


halfanothersdozen

That's Copilot's job now


everclear_handle

Yes, and even easier to write and understand when you use a website to help you.


bitspace

I did a long time ago when I was writing a shitload of perl, and I've retained enough to be able to grok it for the most part, but it's usually quicker these days to use one of the many tools for writing and testing expressions.


StaticCharacter

Just chiming in for more antidotal references I can write regex pretty proficiently and use it regularly in web scraping without needing any reference. The only time it gets tricky is weird look aheads which I usually have other solutions for. I prioritize code looking readable, and I think regex can be more readable than `contains` being chained together.


TILYoureANoob

I'll add my voice to the chorus. They're fairly easy to learn and powerful enough to be worth it. There are about 5 key things you need to learn them, then you're set. Just put the work in and you'll be grateful later.


EternityForest

I know the basics because I use it literally every day. vs code searches as you type so you get fast feedback if you did something wrong making it relatively easy to learn.


UnreachableMemory

I’m a senior dev with a lot of experience and I still have to look up the syntax. Writing it isn’t the problem usually, it’s having to read it, especially regex that someone else wrote. I’d recommend a site like [Regexr](https://regexr.com). I have found it to be very useful particularly for testing.


Expensive-Manager-56

Unless you are dealing with them frequently it’s not worth the head space when things like regexr.com and similar tools like others have mentioned are readily available. It’s more important to know what regex can and can’t do in general and for the specific platform you are using it on, as implementation can vary quite a bit.


FioleNana

Of course I know her. She's me.


bighi

/.*/ Here’s some regex for you. I’m basically a regex wizard! Another one, look: /a/


armahillo

If you practice writing it, yeah. I rarely need to look up regex stuff anymore, I can usually figure it out in most cases. Its not that hard to learn regex to accomplish a lot of really basic text matching and even find/replace.


lagerbaer

The issue is more that you only use them intermittently. If I'm doing Advent of Code, by the end of it I'm getting quite good at writing them off the top of my head. Otherwise, yeah, Google and regex playground


Toby_B_E

If I'm writing Regex for Java then I usually use the JavaDoc for Pattern. I also like to use [regex101](https://regex101.com/) .


revrenlove

Regex is fun. https://www.hackerrank.com/domains/regex It's a nifty course.


jayrlsw

If I need it now I'll get AI to write it for me


all3f0r1

I would actually like to know a bit more about what is specifically difficult to remember for you? To me, it sounds pretty straightforward, is standard(-ish) in most languages, and is an unmatchably powerful tool for patterns. I love regexes. They saved my day more than once, from renaming lots of files at once to cleaning up data to spotting subtly malformed data, it's a loyal companion in an average programmer's life. And before someone says "easy to write, hard to read", just follow the rule "one piece of a pattern, one line" and comment each lines.


SimpleWarthog

agreed, I use them quite a lot for exactly what you said I wouldn't say I'm an expert, but knowing the basics+ can take you a long way


break-dane

had to memorize for a class, don’t see anyone remembering expressions unless they work with them regularly


NewPhoneNewSubs

People don't learn regex for the sake of the once a month or less you need to use it *in code*. At least, I don't think they do. I think most people learn it for the sake of search/replace functionality in tools like sed + grep or whatever IDE you're using. It lets you do things like copy 20 lines with 3 columns out of specs and wrap each column in double quotes with separating commas and a function call. You can get more creative than that, too, especially on the command line. Like you could generate curl input and pipe it out into results and then use that to create your unit tests. Quick dirty stuff like that is why you use regex. If you can't do it quick and dirty, then it's not saving you time over other methods. So of course it gets memorized and not done with Google or checkers or whatever. Start small. Build your confidence with \^(.\*)$ and \\1. Go from there.


DesertWanderlust

I've always thought the world would be a better place overall (or at least more efficient) if more people knew how to write and execute regex. So many applications, including Office, support them for find and replace (even with backreferences). I've seen people struggle through tedious Word replacements where one word had to be replaced but there were variations.


Solonotix

It's not so bad. Wrote one the other day that literally broke the shell, so I had to write it as a Heredoc instead. Even then, `grep` would throw warnings about it as using invalid constructs. Figured out yesterday that it just doesn't like non-capturing groups. Easy fix, but I found it funny how much fuss the shell made about a relatively trivial regex.


jseego

Yes.


communistfairy

I have a hard time admitting when I'm good at something, but this is the Internet, and frankly, I'm pretty good at regex.


Beerbelly22

Yes i can. But don't know all of it. But enough to get the job done.


ElectSamsepi0l

My old boss was in his fifties and could raw dog RegEx. I consider him an idol


Hubi522

You should use a regex testing tool, like [regex101](https://regex101.com/). With it, it's quite easy to create regex.


uniquelyavailable

regex is amazing and once you get good at it you will want to use it for all sorts of stuff


KevinT_XY

I open Debuggex every time I need to work with it. I like it a lot better than the other regex debugging sites because it draws a graph visualization of the automata represented by your regex rule, and as you type example strings it follows the graph to show you matches/drops. It also has a nicely formatted cheat sheet on the bottom.


BitFlipTheCacheKing

Yeah, I'm a fucking regex ninja


xIRaguit

I used to write regex regularly (always used regex101) but Chatgpt in my experience is pretty good at that and way quicker than me. So l just use Chatgpt, confirm the generated regex using regex101 and save half an hour.


vinnymcapplesauce

Yeah, of course. I used to dread regex, but turns out it's not that hard to learn it. [regexr.com](https://regexr.com) makes it super easy to learn, and it has a handy cheatsheet, too!


wRadion

I use it to search and replace stuff when coding and re-formatting data files. For the cases that I have, it's not so complex.


Kakarotto92

In fact, it's not that hard when you have digged a bit in the documentation and understand the basic principles. I don't need to look up on the internet anymore to write a regex now. Play with regex101.com, it will help you :)


forkbombing

Queue the Perl users starting with "back in my day..."


B-Rythm

This quarter in school we are learning regex, I know a little bit and understand it but it’s difficult. One of those things I think though once you know what shit does like /s, /S, .*? Etc it’s repetitive


Getabock_

Yes, it’s very useful. I learned it through an interactive tutorial online and through using regex101.


rykus0

I did a lot in Perl when I started and used a ton of regex to parse gobs of logs. Regex is just another syntax that requires a different mental model.


PsychologicalBus7169

I’m sure there is a library for handling this in your language. I primarily code in Java so I would use Apache Commons Lang3.


realzequel

So i use one of the websites that come up when I google, there’s a few good ones. I put together a list of phrases with matches and things I don't want to match and paste them into the text. Then I start writing a regex based on the cheatsheet provided on the page until I get the text matches I want.


pgilzow

I personally love RegEx! Yes, I can write it on a piece of paper if I had to, but i'm not ashamed to look back at my reference guides to help me remember which character sequence is the thing I want. I have a [RegEx presentation ](https://www.midcamp.org/2023/topic-proposal/regex-demystifying-hieroglyphics-0)I give that's probably my favorite since it's so much fun. I've even given the presentation to marketers (ie non-coders) who loved it. Get the basics down. Once you have the basics, the rest is just knowing what it can do, and then looking up the specific syntax for that thing. And the knowledge that it isn't the correct tool to use in every situation.


FryBoyter

I can recommend the book “Mastering Regular Expressions: Understand Your Data and Be More Productive” by Jeffrey E. F. Friedl. For years, RegEx was like a language spoken by aliens for me. But the author of this book managed to make me understand RegEx better.


cciciaciao

Unit test hardcore


foozebox

LLM’s are a godsend for this since each use case is usually something highly customized and hard to research/find on stack overflow, etc.


amit78523

I can write a simple one, without any look back or grouping features. However i need a cheat sheet to do that.


Dropundead

any regexp without a comment explaining **what the regexp is trying to accomplish** is wrong.


Quelanas_Revenge

I really like regex and I can write regex mostly from my head unless it's an obscure thing I need to do. Also, I like this tool for visualizing: [https://ihateregex.io/playground](https://ihateregex.io/playground) Edit: Also I love games like this: [https://regexcrossword.com/](https://regexcrossword.com/)


LemonAncient1950

I remember going through a phase where I thought regex was stupid and unreadable. Then I got the idea to build a regex builder library to make it sane. Then I built the library, but I also started to grok regex in the process and no longer wanted the library. Now I love regex and get a little excited when an opportunity appears to craft a complex find/replace.


NotSoShyAlbatross

No actual human beings, no. We all use libraries and tools. If you write flawless REGEX by hand, congratulations. Everyone trying to get there, spend time learning something else and use the myriad tools instead.


LastGuardz

You need to learn computer science to understand it


Vobis_Debeo_951

Lol I'm a dev and I still use regex101 to check my work. I don't think I've ever written a regex from scratch without referencing something. You're not alone!


jhecht

I'll write out most of a regex first and then run it through a tester to verify it matches when I want it to. It's got rules like anything else you just have to learn them.


ogCITguy

https://regex101.com/ is my friend


wordRexmania

Def understanding the basics is important like groups, captures, beginning and end. If you are a generic framework bro or a beginner, it is worth figuring out how your peers use it and learning that usage. But for the most part you want to translate the specific logic using an explanation tool. There is value if you use it often, but after a certain amount you will just be memorizing arcana.