Vim and regular expressions

Source: Internet
Author: User
Tags expression engine

Vim's search function is entered/started in normal mode, and Vim's search defaults to regular expressions. So mastering the use of regular expressions and regular expressions in Vim is extremely important for improving the VIM experience. Vim's regular expression help can be obtained from: H usr_27 or: H regexp ( use Help ).

Vim consists of two regular expression engines:

    1. An old-fashioned backtracking-based engine that supports all features.
    2. The new NFA engine will be much faster for some modes, but not all features.

Vim will automatically choose the right engine for you, but if you're having trouble or want to specify an engine specifically, you can add it at the beginning of the pattern:

\%#=0 Force automatic selection

\%#=1 forcing the use of the old engine

\%#=2 forcing the NFA engine to be used

Usually we don't need to care about the regular expression engine, or even focus on the regular expression syntax, just enter a text search. But the following knowledge is very effective in improving efficiency.

Regular expression Basic syntax
An expression meaning
A|b Match A or B
GR (a|e) y Match gray or Grey, while capturing the grouping effect ()
. Match any one character
[ABC] Match any one character: A or B or C
[^ABC] Matches any one character, but does not include a, B, c
[A-z] Match any character from A to Z
[A-za-z] Match any character from A to Z, and from A to Z
^ Match beginning
$ Match end of Line
* Match previous content 0 or more times
? Match previous content 0 or 1 times
+ Match previous content 1 or more times
N Match previous content n times
{N,} Matches the previous item n or more times
{N,m} Match previous item count between N and M
Regular Expressions in Vim

Vim's regular expression is basically the same as the mainstream regular expression, and for those who are familiar with Python,. Net, Perl (the amount) language, they will certainly be very kind when using VIM. But after all, it's a little different.

General Regular Expressions Vim Regular Expression meaning
\ \\ Separating an optional branch
() \(\) Grouping, forming a single matching source
{} \{} Specify number of Matches
{} \{-N,M} Specify the number of matches, but as little as possible
. . Match any one character
* \* Match previous content 0 or more times
? \? Match previous content 0 or 1 times
? \= Match previous content 0 or 1 times
+ \+ Match previous content 1 or more times

Remember that the above Vim-specific wording, and then to write the search expression is very simple and natural.

Settings related to regular expressions in vim
    • Magic: This option controls the behavior of the VIM regular expression, such as the above |, (), {} need to be added in front. For help with Magic, see: H Magic
    • \m: Used at the beginning of an expression as if the Magic option was set
    • \m: Used at the beginning of an expression as if the Magic option was set
    • \v: ' Very magic ', which causes the nonalphanumeric of words other than 0-9a-za-z_ to be interpreted as special characters, under the control of this option, the syntax of the VIM regular expression is particularly similar to the basic syntax of the regular expression mentioned at the beginning of this article. Worth a try.
    • \v: ' Evry nomagic ', which makes only backslashes have a special meaning, other characters are literal characters. This basically closes the regular expression.
    • \c: Case Sensitive
    • \c: not case sensitive
Transferred from: Vimchina

Vim and regular expressions

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.