Getting started with Python regular Expressions (intermediate article) _python

Source: Internet
Author: User

Primary Link: http://www.jb51.net/article/99372.htm

We said in this article, we'll introduce the subexpression, look backwards, and backtrack reference. Until the beginning of this article, in addition to the retrospective reference in some cases can not be replaced, most of the regular expressions you should be written.

1. Sub-expression

The concept of subexpression is particularly well understood. In fact, it is a combination of several characters as a large "character". Not a good idea? Take a chestnut: we want to match the type of character similar to the IP address (for the moment regardless of the rationality of the numerical range, this is left for the study questions after learning). How do we write an expression like 192.168.1.1?

答案一 \d+.?\d+.?\d+.?\d+

Bad, one is too cumbersome, the other is the number of even digits can not control

答案二 \d+{1,3}.?\d+{1,3}.?\d+{1,3}.?\d+{1,3}

Generally, complex but at least can control the number of digits in a reasonable range

答案三 (\d+{1,3}\.){3}\d+{1,3}\.

Using a subexpression, 123. This number plus a decimal point as a whole character, the number of repeated matching of its rules, both concise and good effect. So as long as you enclose a few characters in parentheses, you can use the contents of a parenthesis as a single character, and the outside can add all the metacharacters we've talked about before to control the match.

2. Look forward Backward

Now, we finally came to look forward backwards to this piece. Why did you say that you have finally come here? Do you remember the first example of our initial article?

If you are writing a crawler, you get a page of HTML source code. There is a section of HTML

You want to extract this Hello world.

Import re
key = r " 
 

This regular expression

p1 = r"(?<=

Did (?<= you see it (?= ? The first one? <= indicates that a <H1> must be available before the matched character, and the following? = indicates that the matched character must have a

In short, the character that you want to match is XX, but must satisfy the form is AXXB such string, then you can write regular expression like this

p = r"(?<=A)XX(?=B)"

The string to match is XX. Also, forward lookup backward lookup does not need to occur at the same time. If you want, you can just write to meet one condition.

So you don't have to remember which is looking forward, which is looking backwards. Just remember? <= is followed by the prefix requirement,? = Following is the suffix requirement.

In essence, forward lookup and backward lookup actually match the entire string, that is, AXXB, but returns only one XX. That is, if you want to, you can completely avoid the way forward and backward lookup, directly match the string with the prefix, and then do string slicing.

3. Retrospective reference

Unlike the forward backward lookup, this one sometimes you may not be around the past. In some cases, you also have to use a retrospective reference, so if you want to have a regular expression in a practical application, the backtracking reference is something you should know and master.

Let's start with the first example.

You were supposed to match the content between

p = r"

As a result, you can match all of the title content in the HTML page. The contents of

Say

Did you find the

Import re
key = r " 
 

You can see the effect at the end.

Have you seen \1? Originally that position should be [1-6], but we wrote \1, we have said before, the escape character \ Dry work is to convert special characters into general characters, the general character into special characters. What is the average number 1 being shifted into? Here 1 represents the first subexpression, that is, it is dynamic and varies with the match to the first subexpression in the previous one. For example, the preceding subexpression is [1-6], and 1 is found in the actual string, then the \1 is 1, and if the preceding subexpression finds 2 in the actual string, then the \1 in the back is 2.

Similarly, the \2,\3,.... Represents the second third subexpression.

So a retrospective reference is a "dynamic" regular expression within a regular expression that allows you to match the actual situation.

Intermediate to here, in fact, there are many details of the regular expression has not been written out, there are a lot of meta characters I did not account, but mastered the outline, understand the principle of the rest is similar to the table construction of this kind of live.

The suggestion sees this friend to see "Regular expression must know will", in the elementary article and this article has several examples to draw from this.

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring some help, if there are questions you can message exchange, but also hope that a lot of support cloud Habitat community!

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.