one

Sometimes we need to carry out text processing, search and replace is used more, and sometimes simple replacement can not meet our needs, at this time we can use the regular search and replace, let’s take a look at the following example:

$num = $num; $num = $num; $num = $num; $num = $num

$&
(\d+)

two

For example, what if I want to remove the comma after a number? It can go like this:

three

Again below to see a I encountered in the process of actual development needs, this is I accidentally formatted when writing SQL code, and then will disturb the format, also did not find, call back only to find that, at this time the Ctrl + Z is no longer effective, so want to delete the redundant Spaces, and too lazy to delete one by one, also easy to miss. So we use regular search and replace.

I want to replace all ‘username’ with ‘username’ like this:

The final

It can be seen that the regex used is not complicated, as long as people have learned it, but it can solve a lot of problems, in more complex scenarios, as long as you can write and match the re, you can achieve the function you want, since many people do not know how to use the editor’s re replacement, so this article.

Regular search and replace is generally supported by any modern editor, such as the Sublime Text editor used in my example, the IntellJ whole bucket, and Visual Studio Code, which I also use.

Problem sets

You can’t learn as fast by watching as you can by doing your own regular expressions.

Formalize the following English forms, i.e., each, and. Add a space after it.

William Shakespeare (1564-1616),the foremost writer,prominent dramatist and poet in the European Renaissance.He created a large number of popular literary works,occupies a special position in the history of European literature,has been hailed as “Olympus Zeus in human literature.” He is also known as the four great tragedies of ancient Greece,Aeschylus,Sophocles and Euripides.


Refer to the answer