Regular Expressions (Vim edition) non-greedy matching explanation of regular expressions

Source: Internet
Author: User

I thought I was not particularly proficient in regular expressions, but there was no problem with the basic usage. But recently encountered a problem or let me stretched, thanks to Nango guidance.

Let's start with a general understanding of regular expressions.

Metacharacters

. Any character

^ Beginning of line

$ line End

+ the number of characters immediately preceding it on one or more

? 0 to 1 characters immediately preceding it

* 0 to multiple characters immediately preceding it

{} The number of characters immediately preceding it, optionally. {n}, {n,m}, {n,}, respectively, to show n times, appear n to M times, appear n to many times.

OK, with the above brief explanation, the following question is not difficult to understand.

Scenario reappearance, first look at the source file approximate layout:

<animals exp= "Bird" >
  <animal fly= "1" swim= "1" name= "3"/> <animal fly=
  "1" swim= "1" name= "3" Cmdid= ""/>
  <animal fly= "1" swim= "1" name= "1" cmdid= ""/>
</animals>
In this code, I want to find fields that do not contain cmdid in the animal. Obviously, there is only one line in the room that satisfies the requirements.

Initial Regular expression:

/<animal. *name= ". \{-}" \s\/>
I thought that this expression can, did not expect the result is like this.

The result was really unexpected. I used a greedy match. Should not just match the second line.

There is actually a problem here. The principle of regular expressions is a match. All other principles are based on this foundation. So we can see if the third match. The answer is a match. And there is only one matching scenario.

Since there is only one match, there is no such thing as greed. The so-called non-greed, is built on the basis of matching.

To clarify the problem, let's think about how to solve the problem.

/<animal.*name= "[^"]\{-} "\/>
Using the above paragraph as a regular expression to match, you can achieve the intended purpose. Because there is no "" in the content of the shortest match between ""

This time the use of regular expressions has not been experienced before. I don't know how to analyze the problem at all, but I got the guidance from my predecessors. Not only has the problem been solved, but it has also been understood. I hope this experience will help you.

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.