Programming is quite complex, painstaking, and, often, a monotonous process in which it is not rare to repeat the same or similar effects. To maximize automate and speed up the search and replacement of similar elements in the document, a regular expression system was in programming in programming. It largely allows you to save time and strength of programmers, webmasters, and sometimes representatives of other professions. Let's find out how regular expressions are used in advanced NotePad ++ text editor.
The concept of regular expressions
Before studying the use of regular expressions in the NotePad ++ program in practice, let's find out in more detail the essence of this term.Regular expressions are a special search language using which you can produce various actions on document strings. This is done using special metasimvols, when entering which the search and execution of manipulations on the principle of patterns is performed. For example, in the NotePad ++ point in the form of a regular expression represents any sign of the entire set of existing characters, and the expression [A-Z] is any capital letter of the Latin alphabet.
In various programming languages, the syntax of regular expressions may differ. In the NotePad ++ text editor, the same values of regular expressions are used as in the popular PERL programming language.
Values of individual regular expressions
Now let's get acquainted with the most frequently used NotePad ++ regular expressions:
- . - any single symbol;
- [0-9] - any character in the form of numbers;
- \ D - any character, except for the number;
- [A-Z] - any capital letter of the Latin alphabet;
- [A-Z] - any lower case letter of the Latin alphabet;
- [A- Z] - any of the letters of the Latin alphabet in independence from the register;
- \ w - letter, underscore or digit;
- \ s - space;
- ^ - Start start;
- $ - ending line;
- * - repetition of the symbol (from 0 to infinity);
- \ 4 \ 1 \ 2 \ 3 - the sequence number of the group;
- ^ \ s * $ - search for empty lines;
- ([0-9] [0-9] *.) - Search for two-digit numbers.
In fact, there is quite a large number of symbols of regular expressions, and it is impossible to cover them in one article. Significantly more of their various variations that programmers and web designers are used when working with the NotePad ++ program.
Practical use of regular expressions
Now let's look at specific examples how regular expressions are used in the NotePad ++ program.Example 1: Search
Consider how regular expressions are applied to search for certain elements.
- To start working with regular expressions, go to the "Search" section and in the list that appears, select the "Find" item.
- Before us opens the standard search window in the NotePad ++ program. Access to it can also be obtained by pressing the Ctrl + F key combination. Be sure to activate the "Regular Expressions" button to work with this function.
- We find all the numbers contained in the document. To do this, enter the [0-9] parameter in the search string and click on the "Search Next" button. Each time you press this button, the following digit will be highlighted in the document from top to bottom. Switching to the search mode from the bottom up, which is possible to carry out when using a conventional search method, when working with regular expressions cannot be applied.
- If you click on the "Find all in the current document" button, all search results, that is, digital expressions in the document will be displayed in a separate window.
- And here and the search results derived.
Example 2: Symbol Replacement
In the NotePad ++ program, you can not only search for characters, but also to replace them with regular expressions.
- To start this action, go to the "Replace" tab of the search windows.
- We will make a redirection of external references through redirect. To do this, in the "Find" column, we put the value "href =. (Http: // [^ '"] *) ", and in the" Replace "field -" href = "/ redirect.php? To = 1". Click on the "Replace All" button.
- As you can see, the replacement is successful.
And now let's apply a search with replacement using regular expressions for operations that are not related to computer programming or web page layout.
- We have a list of persons in FULL format with dates of birth.
- Rearrange the date of birth and the names of people in some places. To do this, in the column "Find" write "(\ w +) (\ w +) (\ w +) (\ d +. \ D +. \ D +)", and in the column "Replace" - "\ 4 \ 1 \ 2 \ 3" . Click on the "Replace All" button.
- As you can see, the replacement is successful.
We showed the simplest actions that can be performed using regular expressions in the NotePad ++ program. But with the help of these expressions, professional programmers are carried out and quite complex operations.