1. commands using regular expressions
The most common commands using regular expressions are: /(Search)Command. The format is as follows:
/Regular Expression
Another useful command is: S (replace)Command to replace the regular expression between the first // with the string between the second.
: S/regular expression/replacement string/Option
You can use/Command to practice.
Ii. metacharacters
Metacharacters are special characters. Use metacharacters to expressAny character,Beginning of Line,End of line,Several Characters.
Metacharacters
Metacharacters |
Description |
. |
Match any character |
[Abc] |
Match any character in square brackets. You can use-to indicate the character range, For exampleA-z0-9Match with lowercase letters and Arabic numbers. |
[^ Abc] |
Start with square brackets^Symbol that matches any character except the characters in square brackets. |
\ D |
Matches Arabic numerals, equivalent[0-9]. |
\ D |
Matches any character other than Arabic numerals, which is equivalent[^ 0-9]. |
\ X |
Matches a hexadecimal number, which is equivalent[0-9A-Fa-f]. |
\ X |
Matches any character other than the hexadecimal number, which is equivalent[^ 0-9A-Fa-f]. |
\ W |
Match words and letters, equivalent[0-9A-Za-z _]. |
\ W |
Matching any character other than a word or letter is equivalent[^ 0-9A-Za-z _]. |
\ T |
Match the <TAB> character. |
\ S |
Matches blank characters, equivalent[\ T]. |
\ S |
Matches non-blank characters, equivalent[^ \ T]. |
In addition, if you want to find characters *,.,/, and so on, you must use\Symbol, indicating that this is not a metacharacter, but a common character.
Metacharacters |
Description |
\* |
Match * characters. |
\. |
Match. character. |
\/ |
Match/character. |
\\ |
Matches \ characters. |
\[ |
Match [characters. |
Number of metacharacters
Metacharacters |
Description |
* |
Match 0-any |
\ + |
Match 1-any |
\? |
Match 0-1 |
\ {N, m} |
Match n-m |
\ {N} |
Match n |
\ {N ,} |
Match n-any number |
\ {, M} |
Match 0-m |
Symbol indicating the position
Metacharacters |
Description |
$ |
Match the end of a row |
^ |
Match the beginning of a row |
\ < |
Match the first word |
\> |
Match the word ending |
Example
/Char \ s \ + [A-Za-z _] \ w *; "Find all spaces starting with char, followed by one or more spaces, "The end is an identifier and a semicolon/\ d: \ d" to find a time string in the format of 17:37:01: g/^ \ s * $/d "delete blank rows: s/\ <four \>/4/g" replace all four with 4, however, four in fourteen is not replaced.
3. replace variables
Use in Regular Expressions\(And\)The regular expression is enclosed by a symbol.\ 1,\ 2And other variables to access\(And\).
Example
/\ (A \ + \) [^ a] \ + \ 1 "searches for strings with the same number of a at the beginning and end," such as aabbbaa and aaacccaaa, but does not match abbbaa: s/\ (http: \/[-a-z \. _~ \ + % \/] \ + \)/<A href = "\ 1"> \ 1 <\/a>/"Replace the URL with <a href =" http: // url "> http: // url </a> Format: s/\ (\ w \ + \) \ s \ + \ (\ w \ + \) /\ 2 \ t \ 1 "Change data1 data2 to data2 data1
Iv. Function
In the replace commandS ///You can use a function expression to write the replacement content in the format
: S/replacement string/\ = Function Type
You can use submatch (1) and submatch (2) to reference\ 1,\ 2And submatch (0) can reference the entire Matching content.
Example
: % S/\ <id \>/\ = line (". ")" Replace the id string of each row with the row number % s/^ \ <\ w \ + \>/\ = (line (". ")-10 ). ". ". submatch (1) "replaces the words starting with each line with (row number-10 ). word format. For example, replace the word in the first line with 1. word
V. Differences from Perl Regular Expressions
Differences between metacharacters
Vim syntax |
Perl syntax |
Description |
\ + |
+ |
1-any |
\? |
? |
0-1 |
\ {N, m} |
{N, m} |
N-m |
\ (And \) |
(And) |
Group |
Vi. Regular Expression exercises
Do not speak gossip... The example shows everything. For example, in the following section, I need to replace it with the ubb label.
Vim command mode, enter
: % S/. * src = "([^"] *) "[^>] *>/[img] 1 [/img]/g
Replace
[Img baigu.jpg [/img]
[Img unzip OS .jpg [/img]
[Img baihu.jpg [/img]
[Img languang.jpg [/img]
Explanation:
:
Command Execution status
% S
Search and replace
% S/a/B/g
A is the string to be searched (regular match); B is the text to be replaced; g is the replacement of global search (otherwise, only the first result is processed)
([^ "] *)
It indicates N non-quoted characters, and () indicates that it must be replaced with (1 ,..., 9)
[/Img]
/Escape
What is different from the regular expression of other tools is that () must also be (). No wonder I can't get it all the time.
Related information:
Via http://net.pku.edu.cn /~ Yhf/tao_regexps_zh.html
Vi command function
: % S/* // g replace one or more spaces with one.
: % S/* $ // remove all spaces at the end of the line.
: % S/^ // Add a space on the header of each line.
: % S/^ [0-9] [0-9] * // remove all numeric characters from the beginning of the line.
: % S/B [aeio] g/bug/g change all bag, beg, big, and bog to bug.
: % S/t ([aou]) g/h1t/g change all tags, tog, and tug to hat, hot, and hug respectively (note the use of group and use 1 to reference the matched characters ).
Sed
Sed is short for Stream EDitor. It is a commonly used file-and pipeline-based editing tool in Unix. You can obtain detailed information about sed in the manual.
This is an interesting sed script. We are processing a file called price.txt. Note that these edits do not change the source file. sed only processes each row of the source file and displays the result in the standard output (of course, it is easy to use redirection for customization ):
Sed script description
Sed's/^ $/d' price.txt delete all empty rows
Sed's/^ [] * $/d' price.txt delete all rows that only contain spaces or tabs
Sed's/"// G' price.txt Delete All quotation marks
About magic
Vim has a magic setting. Set:
: Set magic "set magic: set nomagic" cancel magic: h magic "view help
Vim is an editor after all. If a regular expression contains a large number of metacharacters, such as perl ),
It is bound to cause trouble for people who do not understand regular expressions, such as the/foo (1) command. Most people use it to search for the string foo (1), but if it is interpreted according to the regular expression, the searched object becomes foo1.
Therefore, vim stipulates that the metacharacters of a regular expression must be escaped using a backslash. In the above example, if a regular expression is used, it should be written as/foo (1 ). However, it is too troublesome to add backslash characters such. What's more, it's hard to say that some people prefer regular expressions, while others do not ......
To solve this problem, vim sets magic. Simply put, magic is used to set which metacharacters need to be added with a backslash or not.
To put it simply:
Magic (\ m): Except $. * ^, a backslash is required for all metacharacters.
Nomagic (\ M): Except for $ ^, a backslash is required for all metacharacters.
This setting can also be switched temporarily through the \ m \ M switch in the regular expression. The regular expressions after \ m are processed by magic, while those after \ M are processed by nomagic, ignoring the actual magic settings.
For example:
/\ M. * # search for any string/\ M. * # search for string. * (the asterisk is followed by the DOT)
In addition, there are more powerful \ v and \ V. * \ V (meaning very magic): No backslash is required for any metacharacters * \ V (meaning very nomagic): A backslash is required for any metacharacters.
For example:
/\ V (. c) {3 }$ # Find the abcaccadc/\ m (. c) {3 }$ # Find (abc) {3}/\ M (. c) {3 }$ # Find the (. c) {3}/\ V (. c) {3 }$ # Find any location (. c) {3} $
The default setting is magic. We recommend that you use magic settings in vim. You can directly use \ v \ m \ M \ V for special purposes.
The metacharacters used in this article are all in magic mode.
Quantifiers
Vim's quantifiers are not inferior to perl at all.
vim
|
Perl
|
Meaning
|
*
|
*
|
0 or more (matching first)
|
\+
|
+
|
One or more (matching is preferred)
|
\? Or \ =
|
?
|
0 or 1 (matching first ),\? Cannot be in? Used in commands (reverse lookup)
|
\{n,m}
|
{n,m}
|
N to m (matching priority)
|
\{n,}
|
{n,}
|
At least n (matching priority)
|
\{,m}
|
{,m}
|
Up to m (matching priority)
|
\{n}
|
{n}
|
Exactly n
|
\{-n,m}
|
{n,m}?
|
N to m (ignore priority)
|
\{-}
|
*?
|
0 or more (ignore priority)
|
\{-1,}
|
+?
|
1 or more (ignore priority)
|
\{-,1}
|
??
|
0 or 1 (ignore priority)
|
As shown in the preceding table, vim's ignore quantifiers are not like perl's *? +? ?? In this way, the {-implementation is used in a unified manner. This is probably related to ignoring the least commonly used priority quantifiers.
View and curing group
Vim also supports the surround view and curing grouping functions. It is powerful and likes one: D.
For more information about the loop view, see Yurii's "proficient in regular expressions.
vim
|
Perl
|
Meaning
|
\@=
|
(?=
|
Sequential View
|
\@!
|
(?!
|
Sequential negative view
|
\@<=
|
(?<=
|
Reverse View
|
\@
|
(?
|
Negative View in reverse order
|
\@>
|
(?>
|
Curing Group
|
\%(atom\)
|
(?:
|
Non-capturing parentheses
|
What is slightly different from perl is that the pattern of the surround view and curing group in vim is different from perl. For example, to find the bar following foo, perl writes the pattern in parentheses, while vim writes the pattern before the metacharacters.
# Perl statement /(? <= Foo) bar/# vim format/\ (foo \) \@ <= bar
Reference
The vim Help file is very useful. For more information about regular expressions, see the following.
:h pattern:h magic:h perl-patterns