Regular Expression in VIM [conversion]

Source: Internet
Author: User
Tags arabic numbers

From: http://blog.csdn.net/endall/archive/2007/08/29/1764554.aspx

The Regular Expression Function in VIM is very powerful. If you can use it freely, you can perform many unimaginable operations.

If you are familiar with regular expressions of Perl, you can refer to the difference between regular expressions and Perl.

Regular Expression commands

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.

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 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 a 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.
Delimiter indicates the 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
Marker indicates the position symbol †
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.
Replacement variable †

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
Functions †

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
Differences between regular expressions and Perl Regular Expressions †

Differences between metacharacters

Vim syntax Perl syntax Description
\ + + 1-any
\? ? 0-1
\ {N, m} {N, m} N-m
\ (And \) (And) Group

Regular Expression in VIM [conversion]

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.