Using regular expressions in vim

  
        

First, the command to use regular expressions

The most common command to use regular expressions is the /(search) command. The format is as follows:

/regular expression

Another useful command is: s (replace) command, replacing the first regular expression between //with the second //The string between.

:s/regular expressions/replacement strings/options

You can practice with the /command while learning regular expressions.

Second, metacharacters

Metacharacters are characters with special meaning. Use metacharacters to express the meaning of any character, line beginning, end of line, and a few characters.

Metacharacter Overview
Metacharacter Description . Matches any character [abc] Matches any character in square brackets. You can use - to indicate a range of characters, such as [a-z0-9] matching lowercase letters and Arabic numerals. [^abc] Use the ^ symbol at the beginning of the square brackets to indicate that any character other than the characters in the square brackets is matched. \\d matches Arabic numerals, equivalent to [0-9]. \\D matches any character other than Arabic numerals, equivalent to [^0-9]. \\x matches a hexadecimal number, equivalent to [0-9A-Fa-f]. \\X matches any character other than a hexadecimal number, equivalent to [^0-9A-Fa-f]. \\w matches the word letter, which is equivalent to [0-9A-Za-z_]. \\W Matches any character other than the word letter, equivalent to [^0-9A-Za-z_]. \\t matches the <TAB> character. \\s matches whitespace characters, equivalent to [ \\t]. \\S matches non-whitespace characters, equivalent to [^ \\t].

In addition, if you want to find the characters *, ., /, etc., you need to use the \\ symbol in front, indicating that this is not a metacharacter, but just a normal character.
Metacharacter Description \\* Matches * characters. \\. Matches . Characters. \\/Matches /characters. \\\\ matches \\ characters. \\[ matches [ character.

Metacharacter representing the number
Metacharacter Description * Match 0 - any number \\+ Match 1 - any number \\? Match 0-1 \\{n,m} Match nm \\{n} Match n \\{n,} Match n-any number \\{,m} Match 0-m

Symbol indicating position
Metacharacter Description $ Match line end ^ Match line first \\< Match word prefix \\> ; Match word endings

Use case

/char\\s\\+[A-Za-z_]\\w*; " Find all starting with char, followed by more than one blank, " Last Is an identifier and semicolon /\\d\\d:\\d\\d:\\d\\d " Look for a time string in the format 17:37:01: g/^\\s*$/d " delete only Blank line: s/\\<four\\>/4/g " replace all fours with 4, but fourteen Four does not replace 

Third, replace the variable

Use the \\( and \\) symbols in regular expressions to enclose regular expressions, which can be accessed later using variables such as \\1, \\2. The contents of (and \\).

Use Case

/\\(a\\+\\)[^a]\\+\\1 " Find the same number of strings at the beginning and end of a, " like aabbbaa ,aaaccacaaa, but does not match abbbaa:s/\\(http:\\/\\/[-az\\._~\\+%\\/]\\+\\)/<a href="\\1">\\1< ;\\/a>/" Replace the URL with the format of <a href="http://url">http://url</a>: s/\\(\\w\\+\\)\\ s\\+\\(\\w\\+\\)/\\2\\t\\1 " Modify data1 data2 to data2 data1

4. Function

can be used in the replacement command s///Function expressions to write replacement content in the format

:s/replace string/\\=function

In the function, you can use submatch(1), submatch(2), etc. to reference \\1 , \\2, etc., and submatch(0) can refer to the entire content of the match.

Use Case

:%s/\\<id\\>/\\=line(".") " Replace the id string of each line with the line number: % s/^\\<\\w\\+\\>/\\=(line(".")-10) .".". submatch(1) " Replace the words at the beginning of each line with ( Line number -10). The format of the word, " as the word of the 11th line is replaced by 1. word

V. The difference between the regular expression and the Perl regular expression

The difference between the metacharacter
Vim syntax Perl syntax Meaning \\+ + 1- Any number \\? ? 0-1 \\{n,m} {n,m} nm \\(and \\) (and) grouping

VI, vi regular expression exercise < Br>

Don't talk about … the example shows everything, such as the following paragraph I need to change to ubb tag

vim command mode, enter

:%s/.*src=&rdquo ;([^"]*)”[^>]*>/[img]1[/img]/g

Replace with

[img ]gu.jpg [ /img]

[img ]os.jpg[ /img]

[i Mg ]hu.jpg[ /img]

[img ]ang.jpg[ /img]

Explain as follows:

:

Command execution status

%s

means to find and replace

%s/a/b/g

a string to be searched (regular match); b The text to be replaced; g means global search substitution (otherwise only the first result found)

([^"]*)

means N non-quoted characters; Outside () means that the replacement is used later (referenced with 1, …, 9 etc.)

[/img]

Copyright © Windows knowledge All Rights Reserved