Why more programmers should know Regular Expressions?

Credit: Background image by Lorenzo Cafaro from Pixabay.

A regular expression (shortened as regex or regexp) is a sequence of characters that define a search pattern. Usually, such patterns are used by string-searching algorithms for “find” or “find and replace” operations on strings, or for input validation.

If you work with websites, apps, or development of any kind of software on your daily basis, you may end up one day needing to validate a form field or find a specific piece of text inside a content (to replace it for something else or just to use it in the logic of your application). And when that moment comes, there is a possibility that you will face something like this:

^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$

1. It’s a really powerful tool

Regex came a long ago to fill a need that we have in a lot of situations when dealing with data:

2. Learn it once, use it forever

One amazing thing about regex is that you can use it on most programming languages that you may work with, like Java, PHP, Ruby, Python, JavaScript, .NET, C++, and even with software like code editors and so on.

3. It’s easier than it looks

The main thing that stops people from learning regular expressions, is that it seems very complicated and confusing if you look at some patterns for the first time. But once you start getting familiar with each symbol and its relationship with each other, you realize how “simple” it actually is.

/\d{3}/

4. There are amazing tools to help you

Memorizing all the symbols and rules can be tough in the beginning, but that’s why tools are here for.

6. You can understand someone else's code

Fortunately, we live in a world with the internet, and if you need a regex to solve a very common problem, you could simply Google it and voilà, you've found a pattern that hopefully fits your need perfectly.

/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/

5. It can be a stand out skill

Depending on the team that you are working with, and your position on it, knowing regex can be a valuable skill.

Conclusion

Even though regex can be a little frightening at a first glance, it's a powerful tool to have in your utility belt. It can save you a lot of time on manual work, help you automate tasks, improve the quality of your logic, and even reduce some lines of code and headaches.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store