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 expressionAnother useful command is: s (replace) command, replacing the first regular expression between //with the second //The string between.
:s/regular expressions/replacement strings/optionsYou 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-mSymbol indicating position
Metacharacter Description $ Match line end ^ Match line first \\< Match word prefix \\> ; Match word endingsUse 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 replaceThird, 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 data14. Function
can be used in the replacement command s///Function expressions to write replacement content in the format
:s/replace string/\\=functionIn 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. wordV. 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) groupingVI, 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]
Lenovo Y460 users usually hear noise when using the computer, because this notebook is equipped with
u disk is an indispensable tool in our daily life. We often use it to store files. However, in the p
Recently, many users are unable to open the computer device manager, so what is going on? How can we
Recently, some users have reflected Photoshop CS6 installed in the computer, and a window of “Config
You ask me to answer: solve the common problem from XP upgrade to Win7
Register Tomcat as a Windows service
Can you enable multiple Tomcat servers at the same time? How to implement
What should I do if the computer boots up to display client mac addr?
Two tips to teach you how to quickly close the security alert window
The perfect strategy for adding passwords to compressed files
After using IE browser to download files locally, there is no pop-up prompt to notify how to set it?
How to set Thunder as the default download software?
How to deal with CPU fullness in Windows system
Four kinds of Active Directory AD to install as well as configure the way to
Windows 7: Can't change the resolution of the desktop background image
How to close the unnecessary port
Microsoft began sending Windows 7 SP1 test invitations.
System backup and restore operation under Win7
Win2008 R2 and SP1 PS2 can not install the operating system patch solution
[linux study notes] swap virtual memory add and delete configuration
Software version number tells you what is the version number of the software