Refer to the VIM regular expression
A comparison with the previous Linux shell regular expression (bres,eres,pres) can be combined to see
The regular expressions in VIM are powerful and can do a lot of unthinkable things if they are free to use.
I. Commands that use regular expressions
The most common way to use regular expression commands is the /(search) command. The format is as follows:
/Regular Expression
Another useful command is the s (replace) command, which replaces the regular expression between the first///with the second//string.
: s/Regular expression/replacement string/option
You can use / command to practice while learning regular expressions.
two, meta characters
Metacharacters are characters that have special meanings. Metacharacters can be used to express any character , beginning , end of line , character , and other meanings.
Meta characters at a glance
Metacharacters |
Description |
. |
Match any one character |
[ABC] |
Matches any one of the characters in the square brackets. Can be used-represents a range of characters, such as [a-z0-9] matches lowercase letters and Arabic numerals. |
[^ABC] |
Use the ^ symbol at the beginning of the square brackets to match any character except the character in square brackets. |
\d |
Match Arabic numerals, equivalent to [0-9]. |
\d |
Matches any character other than the Arabic numeral, 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, equivalent to [0-9a-za-z_]. |
\w |
Matches any character other than the word letter, equivalent to [^0-9a-za-z_]. |
\ t |
Match <TAB> character. |
\s |
Matches the white space character, equivalent to [\ t]. |
\s |
Matches a non-whitespace character, equivalent to [^ \ t]. |
In addition, if you want to find characters * 、.、/etc., you need to use the \ symbol in front to indicate that this is not a meta-character, but just a normal character.
Metacharacters |
Description |
\* |
Matches the * character. |
\. |
The. Character. |
\/ |
Match/character. |
\\ |
Match \ Character. |
\[ |
Match [character. |
Metacharacters representing the number of characters
Metacharacters |
Description |
* |
Match 0-any one |
\+ |
Match 1-any one |
\? |
Match 0-1 X |
\{N,M} |
Match N-m |
\{n} |
Match N of |
\{n,} |
Match N-any of |
\{,M} |
Match 0-m |
Symbol that represents the position
Metacharacters |
Description |
$ |
Match end of Line |
^ |
Match beginning |
\< |
Match the first words of the word |
\> |
Match word endings |
Examples of Use
/ char \ s \ + [A-Za-z _] \ w *; "Find all words that begin with char, followed by more than one space,
"Finally, an identifier and semicolon
/ \ d \ d: \ d \ d: \ d \ d "find a time string in the format 17:37:01
: g / ^ \ s * $ / d "delete only blank lines
: s / \ <four \> / 4 / g "Replace all four with 4, but four in fourteen is not replaced
third, substitution variables
Using the \ ( and \) notation in regular expressions, you can use variables such as \1,\2 , and so on to access the contents of \ ( and \) later.
Examples of Use
/ \ (a \ + \) [^ a] \ + \ 1 "Find the same number of strings at the beginning and end of a,
"Such as aabbbaa, aaacccaaa, but do not match abbbaa
: s / \ (http: \ / \ / [-az \ ._ ~ \ +% \ /] \ + \) / <a href="\1"> \ 1 <\ / a> / "Replace URL The format is <a href="http://url"> http: // url </a>
: s / \ (\ w \ + \) \ s \ + \ (\ w \ + \) / \ 2 \ t \ 1 "Modify data1 data2 to data2 data1
Fourth, functional
Iv. Functional Type
In the Replace command s/// , you can use a function expression to write the replacement content in the form
: s/Replacement string/\= function
In a functional style, you can use Submatch (1), Submatch (2), and so on to refer to \1,\2 , and so on, while Submatch (0) can reference the entire contents of the match.
Examples of Use
:% s / \ <id \> / \ = line (".") "Replace the id string of each line with the line number
:% s / ^ \ <\ w \ + \> / \ = (line (".")-10). ".". submatch (1) "Replace the word at the beginning of each line with (line number -10) The format of the word,
"As in line 11 with word 1.
v. The difference between regular expressions and Perl
The difference between metacharacters
VIM syntax |
Perl syntax |
Meaning |
\+ |
+ |
Any one |
\? |
? |
0-1 x |
\{N,M} |
{N,m} |
N-m A |
\ (and \) |
and |
Group |
Six, vi regular expression exercises
Gossip Don't say ... Example shows everything, such as the following paragraph I need to change to UBB tag
vim command mode, enter
:% s /.* src = ”([^"] *) "[^>] *> / [img] 1 [/ img] / g
Replaced with
[img] gu.jpg [/ img]
[img] os.jpg [/ img]
[img] hu.jpg [/ img]
[img] ang.jpg [/ img]
The explanations are as follows:
:
Command execution status
%s
means find and replace
%s/a/b/g
A searched string (regular match); B to replace with text; g indicates global search substitution (otherwise only the first result found)
([^"]*)
denotes non-quoted characters N; outside () indicates subsequent substitution (with references of 1,..., 9)
[/IMG]
/need to be escaped
What is different from other tools is that () must be (), no wonder I can't get it out.
Related information:
Via http://net.pku.edu.cn/~yhf/tao_regexps_zh.html
VI Command function
:% s / * / / g Replace one or more spaces with a single space.
:% s / * $ // remove all spaces at the end of the line.
:% s / ^ / / Put a space at the beginning of each line.
:% s / ^ [0-9] [0-9] * // remove all numeric characters at the beginning of the line.
:% s / b [aeio] g / bug / g Change all bags, beg, big, and bog to bugs.
:% s / t ([aou]) g / h1t / g Change all tags, tog, and tug to hat, hot, and hug respectively (note the use of group and the use of 1 to refer to the matched character).
Sed
SED is the abbreviation for Stream editor, which is a file-and-pipe-based editing tool commonly used under UNIX, and you can get detailed information about SED in the manual.
Here are some interesting sed scripts, assuming we're working on a file called Price.txt. Note that these edits do not change the source file, and sed simply processes each line of the source file and displays the results in standard output (which is, of course, easy to customize with redirection):
Sed script description
sed ’s / ^ $ / d’ price.txt delete all blank lines
sed ’s / ^ [] * $ / d’ price.txt delete all lines that contain only spaces or tabs
sed ’s /” // g ’price.txt remove all quotes
VIM Regular expression