[Go] vim regular expression is very powerful

Source: Internet
Author: User
Tags perl regular expression

Needless to say, in the vim of the expression has been very widely used. In the most commonly used/and: s commands, regular expressions are essential. The following is a description of some of the difficulties of regular expressions in vim.

About Magic

There is a magic setting in Vim. The setting method is:

: Set Magic "Settings Magic:set nomagic" Cancel Magic:h Magic "view Help

Vim, after all, is an editor, the regular expression contains a large number of meta-characters if they are referenced (like Perl), it is bound to do not understand the regular expression of people cause trouble, such as/foo (1) command, most people use it to find Foo (1) This string, but if the regular expression to explain, The object to be looked up becomes foo1.

As a result, vim specifies that the meta-character of the regular expression must be escaped with a backslash, as in the example above, if it is true to use a regular expression, it should be written as/foo\ (1\). But, like. * This extremely common meta-character, plus the backslash is too troublesome. And, tune, some people like to use regular expressions, some people do not like to use ...

To solve this problem, vim sets the magic of this thing. To put it simply, magic is to set which metacharacters to add backslashes which are not added. In simple terms:

Magic (\m): except $. * ^ Other meta characters are inverted slashes. Nomagic (\m): All meta characters except $ ^ are inverted slashes.

This setting can also be temporarily switched through the \m \m switch in regular expressions. The regular expression after \m is processed according to the magic, and the regular expression after \m follows Nomagic, ignoring the actual magic setting.

For example:

/\m.* # Find any string/\m.* # find string. * (with an asterisk followed by the dot number)

In addition, there are more powerful \v and \v.

\v (meaning of very magic): No metacharacters are required to add backslashes \v (that is, very nomagic): Any metacharacters must be inverted slash

For example:

/\v (A.C) {3}$ # Find the end of the line ABCACCADC/\m (A.C) {3}$ # Find the end of the line (ABC) {3}/\m (A.C) {3}$ # Find the end of the line (A.C) {3}/\v (A.C) {3}$ # Find Anywhere (A.C) {3}$

The default setting is Magic,vim also recommend that you use the magic settings, when there are special needs, directly through the \v\m\m\v.

The metacharacters used below in this article are all in magic mode.

Quantifiers

Vim's quantifiers are not inferior to Perl.

Vim Perl Significance
* * 0 or more (matches first)
\+ + 1 or more (matches first)
\? or \= ? 0 or 1 (match first), \? Use in command (reverse lookup)
\{N,M} {N,m} N to M (match first)
\{n,} {N,} Min N (match first)
\{,M} {, M} Up to M (match first)
\{n} N Exactly N of
\{-N,M} {n,m}? N to M (ignore precedence)
\{-} *? 0 or more (ignore precedence)
\{-1,} +? 1 or more (ignore precedence)
\{-,1} ?? 0 or 1 (ignore precedence)

As can be seen from the above table, VIM ignores the priority quantifiers unlike Perl's *? +? ?? Instead, it is implemented uniformly using \{-. This is probably not the same as ignoring the priority quantifier is not used.

Surround and cure Groups

Vim also supports the function of surround and cure grouping, powerful, like a look at the interpretation of the Yurii, please refer to the "Proficient regular expression" book.

vim perl meaning
\@= (? = order look around
\@! (?!
\@<= (? <= reverse look around
\@<! (? <! reverse negative look around
\@>
\% (atom\) (?: non-capturing brackets

Slightly different from Perl, the location of the surround view and cure groupings in Vim is different from Perl. For example, finding the Bar,perl immediately following Foo writes the pattern in the parentheses around the look, and Vim writes the pattern before the meta-characters that surround it.

# Perl notation/(? <=foo) bar/# vim/\ (foo\) \@<=barvim Regular Expressions Write

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] starts with the ^ symbol in square brackets, which means that any character other than the character in square brackets is matched.
\d matches the 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 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 matches the <TAB> character.
\s matches the white space character, equivalent to [\ t].
\s matches non-whitespace characters, equivalent to [^ \ t].
\a all the alphabetic characters. equivalent to [a-za-z]
\l Small Letter [A-z]
\l non-lowercase letters [^a-z]
\u Capital Letter [A-z]
\u non-capital letters [^A-Z]

Metacharacters representing the number of characters
Metacharacters description
* Match 0-any one
\+ Match 1-any one
\? Match 0-1 X
\{n,m} matches N-m
\{n} matches N
\{n,} matches N-any
\{,m} matches 0-m
\_. Match all characters that contain line breaks
\{-} indicates that the previous character can occur 0 or more times, but the fewer characters are matched if the entire regular expression can match successfully
\= match an optional item
\_s match spaces or break
\_[]

Metacharacters description
\* matches * characters.
\. The. Character.
\/Match/character.
\ \ matches \ characters.
\[matches the [character.

Symbol that represents the position
Metacharacters description
$ Match Line End
^ Match beginning of Line
\< Match Word First
\> Match Word endings

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.

Lazy mode
\{-n,m} like \{n,m}, repeat as few times as possible
\{-} matches the item in front of it one or 0 times, as little as possible
\| "or" operator
\& Juxtaposition


function type
: 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.

What is the difference from a Perl regular expression?
The difference between metacharacters
Vim Syntax perl syntax meaning
\+ + 1-any one
\? ? 0-1 x
\{n,m} {n,m} n-m
\ (and \) (and) group

For example:
1, remove all line trailing spaces: ":%s/\s\+$//". "%" means to replace the entire file scope, "\s" denotes whitespace characters (spaces and tabs), "\+" matches the preceding characters one or more times (the more the better), "___fckpd___0rdquo; matches the end of the line (using the \___fckpd___0rdquo; Denotes a simple "___fckpd___0rdquo; character"; The replaced content is empty; Because a line can be replaced at most, no special flags are required. This is still relatively simple. (/<space><tab>)
2, remove all blank lines: ":%s/\ (\s*\n\) \+/\r/". This is a lot more "\ (", "\"), "\ n", "\ R", and "*". "*" represents 0 or more occurrences of the preceding character (here, "\s") (the more the better; use "\*" to denote simple "*" characters, "\ n" for line breaks, "\ r" for carriage returns, "\ (" and "\)" to group the expressions so that they are considered an integral whole. Thus, the full meaning of this expression is to replace the successive newline characters (including the contiguous white space character that may precede the line break) with a single line break. The only thing that is special is that "\ n" is used in the pattern, but "\ n" is not used in the replaced content and only "\ r" is used. The reason is history, and if you are interested, you can check out ": Help Nl-used-for-nul".
3, remove all the "//" Comments: ":%s!\ s*//.*!!". The first thing to notice here is that the delimiter is replaced by "!" because the "/" character is used in the pattern or string part, and the words "/" are written "/" Each time the "/" character is used instead of the other delimiter, and the command above is written as ":%s/\s*\/\/.*//" and is less readable. The command itself is quite simple, and people who have used regular expressions know that "." Matches any character other than the line break.
4, remove all the "/* */" Comments: ":%s!\s*/\*\_.\{-}\*/\s*!!g". This is slightly more complicated and uses several less commonly used Vim regular expression features. “\_.” Matches all characters including line breaks; "\{-}" means that the previous character can occur 0 or more times, but the number of matched characters is better when the entire regular expression can be matched successfully; the flag "G" indicates that a row can be matched and replaced multiple times. The result of the substitution is a space to ensure that expressions such as "int/* space not necessary around comments */main ()" are still valid after the substitution.

: g/^\s*$/d Delete only blank lines
: s/\ (\w\+\) \s\+\ (\w\+\)/\2\t\1 change data1 data2 to Data2 data1
:%s/\ (\w\+\), \ (\w\+\)/\2 \1/change Doe, John to John Doe
:%s/\<id\>/\=line (".") replaces the ID string of each row with the line number
:%s/\ (^\<\w\+\>\)/\= (Line (".") -10). ".". Submatch (1)
Replace the word at the beginning of each line with (line number-10). The word format, such as Line 11th, is replaced by word 1. Word
Sort:/ob/+1,$!sort

[Go] vim regular expression is very powerful

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.