DEV Community

Discussion on: How do you regex?

Collapse
 
dan_starner profile image
Daniel Starner

For building & explaining regular expressions, I always use regex101.com/. It's a great tool that really break down how your regular expression will work, which is very useful if it can get complex. It also has multi-language support to match the regex flavors across different major languages & distributions.

For learning regex, I usually just Google and dive through StackOverflow posts similar to what I'm trying to do. If I write a regex that is more complicated than just some alphanumeric grouping, I try to comment it with some human text such as capture the ID from the post slug or something of the sorts...not very in-depth, but πŸ˜…

Regular expressions are most commonly used when I am validating string structure, which could be form inputs, hostname information, or CSV data. If I am parsing strings, my first go-to for basic cases is to do some combination of .split() and .join on the resulting array, as its a bit easier to reason about and requires no understanding of regular expressions, just some basic logic. I tend to fall back to regular expressions if the string parsing is more complicated, has more than one match, or requires some more advanced substitution.

Collapse
 
ben profile image
Ben Halpern

For learning regex, I usually just Google and dive through StackOverflow posts similar to what I'm trying to do.

Just curious, have you ever considered taking on a dedicated approach to learning Regex β€” like a book or course?

Definitely not a judgment, just a curiousity.

Collapse
 
dan_starner profile image
Daniel Starner

No judgements found!

I tend to learn on the fly as I'm trying to implement something, so it probably means I am going to immediately use the idea if I am learning something new concerning regular expressions. This is also predicated on the fact that I've been using them with regular consistency for 4-5 years so I have a firm foundation in the basics. If I am looking something up, its usually related to Named Groups (I always forget the exact syntax), Lookarounds, and Conditions. The latter two only get used in very rare & complex scenarios, so I don't bother committing them to memory fully.

When I was just getting started, I just spent a day on regex101.com/ trying to build different expressions to match different strings to see how it would work. This learning was compounded by the fact that Django used to exclusively use regular expressions for route matching, so it "forced" me to work with them.