Why do developers need to know Regular Expression?

Tram Ho

Regular Expressions, also known simply as Regex, is one of the most powerful and most widely used programming tools.

Everything from simple to complex can be solved by Regex, such as checking email is valid or not, or more difficult like refactor a complex block of code, search and replace a number. specific patterns, etc.

Only then can we see that Regex is an extremely powerful tool, can be used in code, can be used with an editor, and if you are a real developer, it is indispensable in your inventory.

Most developers involved in the * nix world are more familiar with Regex than developers in other areas, because they will have to frequently use commands like sed / awk.

I had a funny story like this in the past. I and a guy work from a pair programming company together.

So that guy spent an entire afternoon asking me about Regex, and I could do nothing but sit and guide him the whole afternoon.

What does this story tell?

There are still quite a few developers who are confused about Regex, some people copy the Regex passages into their code without even knowing it is Regex, others even know Regex’s presence but do not care how to write it. it.

My opinion is that anything, if you use it, you must understand it, and that is the reason I wrote this article.

What is Regex?

In a concise and simple way, Regex is a way to describe a complex pattern used for search (search pattern) by a string. For example, you can check strings that contain letters or numbers, or you can go deeper than checking number of characters, position of characters, upper case, lower case, and more.

Learn once to use it anywhere!

Regex can be used in almost all languages. Basic Regex learning is quite simple, you just need to learn the basic expressions and combine them. In detail, I will not explain each expression in this article. You can go to https://regexr.com/ , to learn and try on the spot. I think it only takes about 30 minutes for you to write a simple Regex for yourself.

For example:

Let me explain to you one by one.

  • ^: This is the expression that represents the beginning of a line
  • [0-9]: This is the expression that shows the character exists in the range 0 to 9
  • +: This is the expression that shows the existence of 1 or more characters that match the previous expression
  • $: This is the expression representing the end of a line

In a nutshell, the above Regex is used to search for lines containing only numeric characters.

Practical use like?

Here is a real-life example that I often use Regex. I have a data file that looks like this.

Now I want to insert this pile of data into a table in the database named Pet with two data fields named name and kind like this.

INSERT INTO Pet (name, kind) VALUE ('Max', 'Dog');

The usual way would be to type the line of code insert and then copy for the rest of the line, assuming the file has 10000 lines, it will be copied until Tet.

If using Regex I will use the following.

There will still be cases where Regex does not need to be used

Although I like Regex a lot, but honestly it is quite easy to learn Regex in the basics, but if you want more it is really difficult, very difficult. And you will have to deal with some pretty headaches like character classes, quantifiers, alternation, etc, but I won’t write it here.

You just need to remember at the beginning of this sentence.

The purpose of using Regex is to save you time and effort. If you take longer to debug a Regex than you should use the normal method then you should stop and use the regular method is better.

Regex is a great tool, but that doesn’t mean we use it all the time.

If there exists a simpler way or does not require Regex then don’t try to use Regex to be smarter. Because Regex is hard to read, hard to debug, and there may be some edge cases that if you don’t understand Regex you may encounter.

And overusing Regex will be the easiest way to make your coworkers angry.

Summary

Hope through this article you have a different view about Regex. If you want to become a real developer, start learning about Regex right now. If you already know Regex and you know someone who doesn’t know Regex, teach them.

In addition, I would like to introduce another good article about Developer should know about Regex of TheFullSnack blog that you should also read because there are illustrations of the author’s drawing quite vividly.

Original article: Codeaholicguy

Share the news now

Source : Viblo