DEV Community

Cover image for Step up your Regex game
blakewood84
blakewood84

Posted on

Step up your Regex game

Learning Regex (Regular Expressions) is not fun or easy at first, but it is a beyond important skill when strategizing solving problems with strings. Here is a brief overview on ways to best learn regex, tutorials on regex, and cheat sheets.


Alt Text

When I first saw Regex I immediately said what the heck is this!? I knew right off the bat it required some time and concentration to get it right and wrote it off as a “I’ll try this later” kind of deal. I kept pushing it off and thought, hey I’ll just code my way out of problems, no big deal. Was I ever wrong! In one of my most recent technical interviews, guess what? Regex came up!

Regex is very important, not only for solving problems that manipulate strings, but are widely used in text-editors, to search for data, filtering results, matching passwords, matching emails, validating forms, etc. You can go from solving a string manipulation problem on CodeWars or HackerRank in n time to cutting that time in half with Regex! This goes to show you how valuable Regex really is!

So began my process to understanding Regex completely. I don’t want to strive to just solving problems by brute force, but rather I’d like to be efficient as possible. Hence the quote, “think smarter not harder”.

Below I felt are the best ways to obtain that Regex knowledge, going from beginner to intermediate, and well advanced? If you want to be advanced you need to practice Regex on the reg, and if you set your mind to it you will achieve it.

First Steps:

You understand you need to learn Regex but where do you start??

Step 1 — Take a tutorial:

Well praise God for DuckDuckGo (I only use DuckDuckGo) and Google, here are some pretty cool sites that offer a step-by-step learning process that will surely help. I like the fact that a few of these offer quizzes at the end.

  1. RegexOnehttps://regexone.com/
  2. Learn Regexhttps://learn-regex.com/
  3. RegularExpressions.Info Tutorialhttps://www.regular-expressions.info/tutorial.html
  4. Mozilla Regular Expressions (Use this to see how to code with Regex for beginners) — https://www.regular-expressions.info/tutorial.html
  5. W3Schools JS Regex Tutorialhttps://www.w3schools.com/jsref/jsref_obj_regexp.asp
  6. Javascript.info Regular Expressions — https://javascript.info/regular-expressions

Step 2 — Practice on CodeWars or HackerRank:

Definitely start immediately on using Regex to solve problems when strings are present. The best place to do this is to go to CodeWars and HackerRank and start from the beginning. Whenever I start learning something new or need to refresh, I create a new account on these and start from the beginning and I always learn something new coding.

Another thing to note is it helps to solve these problems first on something familiar and helpful to you uniquely. I often create a new Repl.it and solve the problem there, then paste the solution in the HackerRank or CodeWars editor to solve. Just drag some of the arrays or data their using for the initial tests and run your solution on them. There are other online code editors such as JS Bin, PlayCode, or JS Fiddle that work great as well.

Here are a few sample problems that should be simple enough to use Regex on:

  1. CodeWarsYour order, please: https://www.codewars.com/kata/your-order-please
  2. CodeWarsJaden Casing: https://www.codewars.com/kata/5390bac347d09b7da40006f6
  3. CodeWarsCollection of Strings (problems involving strings)_: https://preview.codewars.com/collections/strings-130
  4. HackerRankSolve Regex (list of Regex Problems): https://www.hackerrank.com/domains/regex

Step 3 (optional) — Find videos on YouTube, Udemy, or other sites:

If you want to be good at something and you need a visual kick or walk through just look up some videos to help follow along. This is just a suggestion and everyone has their own unique preference for people they like to follow along with. Get busy and do some searching!

Second Steps:

You’ve learned the regex basics but where to go from here? Start your own projects immediately! The only way to really learn and commit those things to memory is straight up creating or building some projects yourself with 0 help from tutorials or anyone else. This is the only way to learn.

Here are some ideas for first projects:

  1. Create a Regex Matcher — create a project where you can test a regex on a string, highlighting the exact matches on the string with the regex you provided. Give a simple “Success!” or “Failed” somewhere based on strings you’re evaluating to match.
  2. Create a search filter bar: return only data sets that match the input given of a string. An example of this would be Netflix or DisneyPlus. Return only the videos that match or similar to what you are searching for in the search bar. There are a million ways to come up with your own version of this.

Useful Tools:

Keep practicing Regex. Here are some useful tools and cheat sheets to promote your growth!

Cheat Sheets:

Use these when referencing Regex on the go when using in your own projects and CodeWars or HackerRank:

  1. Regex TutorialA quick cheat sheet by example by Johnny Fox: https://medium.com/factory-mind/regex-tutorial-a-simple-cheatsheet-by-examples-649dc1c3f285
  2. Ultimate Regex Cheatsheethttps://www.keycdn.com/support/regex-cheatsheet

Practicing your Regex:

Use these Regex engines to practice on. Will come in handy for writing out your Regex before you drop it into code when learning.

  1. Regexr.comhttps://regexr.com/
  2. Regex101.comhttps://regex101.com/

I hope you were able to get some value out of everything in this article. Just know that if you are struggling in programming or coding, there is always a solution and you can do it. No matter what never give up and keep growing. Everyone is unique, and the way you learn is to, you just have to make it fun and adapt to what works for you!

If you are someone who's worked with Regex before or have some knowledge on the subject, I have a few questions for you! What sites helped you grow/learn with Regex? What projects did you work on that helped as well? Please share below in the comments because I'd definitely like to know and I'm sure others would to!

Bless

Discussion (4)

Collapse
filatovv profile image
Yuri Filatov

Thanks for the article, it is really useful. But you know, sometimes it doesn't work. I' ve also learnt more about regex and practised on these websites, but when I come to some difficult (big) regex, then I make mistakes. I don't get why, cause I guess, I understand everything, but everything is not enought , I guess... , so Idk what else to read or use not to make those mistakes... or maybe you have a tip?

Collapse
crocsx profile image
Federico Giacomini

To be honest, I think it's the kind of thing where you must practice a lot to get good at. I would chain exercises. So to add some useful links

Exercices :
regexone.com/problem/matching_deci...
regexcrossword.com/
alf.nu/RegexGolf

Useful regex to dissect and learn
projects.lukehaas.me/regexhub/

Visualizers:
regviz.org/
debuggex.com/

Collapse
blakecodez profile image
blakewood84 Author

Nice links!

Collapse
blakecodez profile image
blakewood84 Author

Practice! It takes time 😁