Everyone can use regular expressions

Original link: https://lutaonan.com/blog/everyone-can-use-regexp/

There’s a joke about regular expressions that goes like this: we had a problem, solved it with regular expressions, and now it’s two problems.

This joke illustrates how complex and sometimes difficult to maintain regular expressions are. If we are new to an existing project and regular expressions appear in it, our first reaction must be a headache. Because regular expressions are not easy to read, it usually takes several readings to get a little bit of what he meant.

Including me, for a long time, my attitude towards regular expressions was to use them if they could. Until I finished the book Mastering Regular Expressions a few years ago, my attitude towards regular expressions changed dramatically.

The first section of the first chapter of this book is titled “Solving Practical Problems”, and in this first section I learned the magic of regular expressions. It also made me realize that you don’t necessarily need regular expressions, but you have to master the basics of regular expressions, overcome your fear of it, and turn it into one of your tools. Later, when you encounter another problem, you will realize that regular expressions are one of the elegant solutions. When you use regular expressions as a tool for yourself, you will find that some problems that you used to think you need to write a program to solve are actually solved with just one line of regular expressions.

For example, I usually want to convert a JSON file into a JavaScript object, and I want to remove the double quotes around keys in the JSON. In the past, I might first Google “JSON to JavaScript object” to see if there is a ready-made tool, but now I will first think that this problem can be solved with regular expressions and the replacement function that comes with the editor.

Suppose this is the JSON we want to convert:

 {
"name" : "Randy" ,
"children" : [
{
"name" : "Sandy"
} ,
{
"name" : "Mandy"
}
]
}

My idea would be: first, I want to match all keys, and the characteristics of keys start with double quotes, end with double quotes, and must be followed by a colon, so I can use this regular expression to find all keys come out:

 ".+":

Here . means any character, + means match one or more.

One of the powerful features of regular expressions is that you can wrap a match in parentheses to turn it into a group, and these groups will be numbered, starting with 1. In this way, after matching, we can use the form of $1 to take out the value of the matched group and then process it. In this example, we use the replacement function of VS Code to match the key, and then put it in a group. When replacing, use $1 to get the value of the key and use it as a replacement:

 "(.+)":

There are many more examples, readers can draw inferences. The purpose of this article is not to teach you the usage of regular expressions, but to make you realize that regular expressions are a very useful tool, which does not have to be used in projects. In some daily text processing tasks, It can play an unexpected role. The basic usage of regular expressions is not difficult to remember, and you will be familiar with it after using it a few times. Of course, for more advanced usage, you need to read the manual more.

This article is reprinted from: https://lutaonan.com/blog/everyone-can-use-regexp/
This site is for inclusion only, and the copyright belongs to the original author.