Get the App
SLTechnology News&Howtos  ›  Servers  › 

Detailed explanation of finding and replacing and using regular expressions in Vim

Shulou Source: shulou.com Published: 2022-06-02 02:20:55 09月23日 Update

Preface

Regular expression is a powerful tool in the field of text processing, which can improve the ability of text processing exponentially. If a text editor does not support regular expressions, then it is not a modern editor. This is not a lie.

The following article mainly introduces the relevant content about Vim search and replacement and the use of regular expressions, and shares it for your reference and study. Let's take a look at it.

Simple replacement expression

: [range] s/from/to/ [flags]

Range: search scope. If no scope is specified, it acts on but forward.

: 1 10s/from/to/ 10s s/from/to/ to / means to search for substitutions between lines 1 to 10 (including lines 1, 10);: Magazine means to search for replacements only on line 10;:% to search for substitutions in all lines; and 1 to search for substitutions from lines to / ditto.

Flags has the following four options:

C confirm, ask before each replacement; e error, do not show errors; g globle, do not ask, replace the whole line. If the g option is not added, only the first matched string on each line is replaced; I ignore, case is ignored.

These options can be combined, such as cgi for case insensitivity, full line replacement, and ask before replacement.

Regular expression

1. Metacharacter

Metacharacter

Metacharacter description. Match any character [abc] match any character in brackets, and use-to indicate the character range. For example, [a-z0-9] matches lowercase letters and numbers [^ abc] matches any character except characters in square brackets\ d matches Arabic numerals, which is equivalent to [0-9]\ D matching any character except Arabic numerals, equivalent to [^ 0-9]\ x matching hexadecimal numbers, equivalent to [0-9A-Fa-f]\ X matching any character except hexadecimal digits Equivalent to [^ 0-9A-Fa-f]\ l matching [Amurz]\ L matching [^ amurz]\ u matching [Amurz]\ U matching [^ Amurz]\ w matching word letters, equivalent to any character other than the letters of [0-9A Zamurzz]\ W matching words, equivalent to [^ 0-9Amurzz]\ t matching characters\ s matching blank characters, equivalent to [\ t]\ S matching non-white space characters Equivalent to [^\ t]

Some ordinary characters need to be changed.

Metacharacter description\ * matches * character. Match. Character\ / match / character\ match\ character\ [match [character\] match] character

Metacharacters that represent the number of metacharacters

Metacharacter description * match 0-any\ + match 1-any\? Match 0-1\ {NM M} match n M m\ {n} match n\ {n,} match n-any\ {, m} match 0 m m

A metacharacter that represents a position

Metacharacter description $match line end ^ match line beginning\ match word suffix

2. Replace the variable

Regular expressions enclosed in\ (and\) in regular expressions can be used later with variables such as\ 1 and\ 2 to access the contents of\ (and\).

Examples

Delete the space at the end of the line::% s /\ sdelete the extra space at the beginning of the line:% s / ^\ sdelete the extra space at the beginning of the line:% s / ^\ sdelete the blank line with no content:% s / ^ $/ or g / ^ $/ d Delete the blank line that contains spaces:% s / ^\ sdelete the blank line from beginning to end with a space or TAB: % s / ^ [|\ t] * $/ / or g / ^ [|\ t] * $/ d put all the strings "abc" in the text. Replace "xyz" with "xyz". Abc "can be written as follows:% s/abc\ (. *\) xyz/xyz\ 1abc/g:%s/\ (abc\)\ (.*\)\ (xyz\) /\ 3\ 2\ 1hand g

Summary

The above is the whole content of this article, I hope that the content of this article can bring some help to your study or work, if you have any questions, you can leave a message and exchange, thank you for your support.

Tags: Characters metacharacters regularities expressions content words spaces searches letters numbers text blank lines ranges hexadecimal variables size strings square brackets white space articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Linux vpn Shulou Tech Info Docker