VIM Regular expression

Source: Internet
Author: User

The regular expressions in VIM are powerful and can do a lot of unthinkable things if they are free to use.

If you are familiar with Perl's regular expressions, you can refer to a section directly.

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 start with char, followed by more than one blank,
"Last is an identifier and a semicolon
/\d\d:\d\d:\d\d "Find a time string such as 17:37:01 format
: g/^\s*$/d "Delete only blank lines
: s/\<four\>/4/g "Replaces all four with 4, but four in fourteen does not replace
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                                          ] finds the same number of strings at the beginning and end of a.
"such as AABBBAA,AAACCCAAA, but does not match Abbbaa
: s/\ (http:\/\/[-a-z\._~\+%\/]\+\)/<a href= "\1" >\1<\/a>/" replace URL with <a href=" Http://url ">http Format of the://url</a>
: s/\ (\w\+\) \s\+\ (\w\+\)/\2\t\1 "Change data1 data2 to Data2 data1
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 row with the line number
:%s/^\<\w\+\>/\= (".") -10). ".". Submatch (1) "Replace the word at the beginning of each line with (line number-10). The format of the word,
"Word like line 11th is replaced with 1." Word
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, input

:%s/.*src= "([^"]*) "[^>]*>/[img]1[/img]/g

Replaced by

[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/*$//removes all whitespace from the end of the line.

:%s/^//Add a space to each outfit.

:%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 changes all tags, tog, and tug to hat, hot, and hug (note the use of Group and 1 references to previously matched characters).

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 empty lines

Sed ' s/^[]*$/d ' price.txt Delete all lines that contain only spaces or tabs

Sed ' s/'//g ' price.txt Delete all quotes

Vim Regular expression (RPM)

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.