How to stop being afraid and falling in love with regular expressions

At work and in everyday life, we are literally surrounded by texts: books and articles, letters and chats, web pages and program code. Why, biological life itself is based on DNA. And for effective work on searching or modifying text, a powerful tool has long been invented - regular expressions, or regex. However, for a number of reasons, not all use them, or deal with them only when forced when absolutely necessary. In this article, I will try to remedy this sad situation.









In order to find functions, to call them together and to forge them with a single black will.



There are already a number of good articles on Habré that will help you understand the basics:





, , .







, . , :







^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$&^()*])[a-zA-Z--0-9!@#$&^()*]{1,}$

      
      





. , . : « , ?».









, regex.





. Notepad++ (, – ).

, Ctrl+f, «Regular expression» !









Notepad++.



1.



(, ) « »











! «».

– . ( «Math Case»), . «», «», «» . , . , . , .







, , – . . , .







The first step has been taken, and nothing prevents us from moving on.









2. Wildcard characters



Another common opportunity - it is a wildcard (wildcard) an asterisk " * ". It replaces any number of any valid characters. The most common example is searching for files.







Let's say we want to find photographs of a seaside holiday. To do this, enter " * .png " in the search bar and get a list of all photos in the folder:









 .png
 .png
--, .png
 .png
 , -.png

      
      





Already good. What happens if we add the word "sea" before or after the asterisk?

sea ​​* .png



 .png

      
      





* sea.png



--, .png

      
      





The next option will return what you need:

* sea * .png



 .png
--, .png
 , -.png

      
      





«**.png» — , – - ( «.png» - «»), .







, , , . — «.*».









3. «.*»



– « , 0 ». « ». « ».







, , , , . , : «.*» «.*»»:











215 . …











, . «|»: :

(.*|.*)











, — 213. , , , « , .» .







, , :

(|)











. , . , , . , .







, , regex-.









Saruman understands the power in his hands and is not afraid of it. Therefore, he is always aware of all matters.



4. Number of repetitions or quantifiers



We have already met one quantifier - the asterisk. There are several more ways to set the amount:

{4} exact amount
{1,5} range
{2,} from number to infinity
{, 7} from 0 to number


Some commonly used ranges have their own symbols:

* or {0,} any quantity
? or {0,1} zero or one
+ or {1,} there is at least one


Let's ask how often the same word is mentioned in one paragraph:











2 — «(.*){2}» — 7 , — «(.*){3,}» — . «» 3 , «» «» «».









5.



( ) — . «\d», «\w», «[abc], «[1-9], «[.,?:;]».

, , :

«\d+» «\d{1,}» – :











. (37.), (. .) , 4 — \d{4}











1070 , 1421 — 351 .







.















: , , – , — . : . . «Replace»:











Now you can read a book with a familiar translation. But be careful: a search pattern can match not only the desired word, but also a longer one containing the desired word. Then, when replacing "dacha" with "village", we can unexpectedly get "village" or "televillage". It is advisable to first search for the same template and check the results. Complicate the template for a more precise search. In the case of "dacha" we can add a space before the word and a space or punctuation marks at the end: " dacha [.,!?;:] ".



Practice of use



To create, edit, or explore regular expressions, I recommend using an online editor you like. For me, the most convenient was REGEX101 :









The ring finds all the function definitions in ISO C99 and is therefore so powerful.



At the top, we enter a regular expression, and at the bottom, the text we are searching for. The editor automatically highlights the found matches, when you hover over a part of the expression, hints explaining the meaning appear, and many other useful functions. To replace, select "Substitution" in the left panel.



Conclusion



  • Use search more often for convenience and speed up your work.

  • Use an asterisk when searching for files by creating simple templates.
  • Start using simple regular expressions in your day-to-day work as you gain experience and gradually explore new features.


Of course, this article only touches the very top. Many useful features and possible problems were left unattended. But if you have a desire to plunge into the world of regex more thoroughly, in addition to reference books and tutorials on the Internet, you can read the following articles on Habré:





Today I can no longer imagine my work without regular expressions, as well as without the blind printing method. All you had to do was start using it. Which is what I wish for you.










All Articles