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.
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.
You understand you need to learn Regex but where do you start??
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.
- RegexOne — https://regexone.com/
- Learn Regex — https://learn-regex.com/
- RegularExpressions.Info Tutorial — https://www.regular-expressions.info/tutorial.html
- Mozilla Regular Expressions (Use this to see how to code with Regex for beginners) — https://www.regular-expressions.info/tutorial.html
- W3Schools JS Regex Tutorial — https://www.w3schools.com/jsref/jsref_obj_regexp.asp
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:
- CodeWars — Your order, please: https://www.codewars.com/kata/your-order-please
- CodeWars — Jaden Casing: https://www.codewars.com/kata/5390bac347d09b7da40006f6
- CodeWars — Collection of Strings (problems involving strings)_: https://preview.codewars.com/collections/strings-130
- HackerRank — Solve Regex (list of Regex Problems): https://www.hackerrank.com/domains/regex
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!
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:
- 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.
- 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.
Keep practicing Regex. Here are some useful tools and cheat sheets to promote your growth!
Use these when referencing Regex on the go when using in your own projects and CodeWars or HackerRank:
- Regex Tutorial — A quick cheat sheet by example by Johnny Fox: https://medium.com/factory-mind/regex-tutorial-a-simple-cheatsheet-by-examples-649dc1c3f285
- Ultimate Regex Cheatsheet — https://www.keycdn.com/support/regex-cheatsheet
Use these Regex engines to practice on. Will come in handy for writing out your Regex before you drop it into code when learning.
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!