The guide to the use of Python _python

Source: Internet
Author: User

Last time a lot of friends to write a text screen to use regular expression, in fact, is not I do not want to use (I am using is not a lot, have seen me before the crawler know, I directly use BeautifulSoup Web tags to find content, because easy to understand also convenient,), But it is very difficult to use good mastery (see the regular table should know, inside the symbol corresponding to the rules of the method are many, very flexible, for contact with the program soon friends are likely to waste a lot of time in the programming process, today I will often use a simple introduction, if not very special basic cover use.

1. Simple introduction of regular

First, you have to import regular methods. The regular expression is a powerful tool for handling strings, has its own independent processing mechanism, and may not be as efficient as the Str band method, but the function is very flexible. Its running process is to first set a matching rule ("You want the content + regular grammar rules"), put in the string to match, through the regular internal mechanism can retrieve the information you want.

Several kinds of postures used in 2.findall

Basic structure: Nojoke = Re.findall (r ' Match rule ', ' wish string to retrieve ') Nojoke is the result that we finally pass the regular return, re regular findall find all r logos to represent the following regular statements (so that when the code is good to read), Let's take a look at a few examples to get a good insight

This code is to find all the bi in the retrieved string and return it as a list, often using the number of times the uniform character appears. Keep looking next.

This adds a symbol ^ to match the return of a string that begins with the ABI, or whether the string starts with the ABI.

Here is a string returned with the $ symbol at the end of the GBI to determine if the string ends.

Here [...] Matches the values of a and f in parentheses, or B and F, or C and F, to return the list.

"\d" is a regular grammar rule that matches the return list between 0 and 9, and it should be noted that 11 will return as the string ' 1 ' and ' 1 ' instead of returning the ' 11 ' string, and remember that this is a big hole.

Of course, the solution is you have to take several numbers to write a few \d, the above demo to take a string of 3 digits, here shows the regular flexibility on the one hand.

Here small d means to take the number 0-9, Big D means not the number, that is, outside the number of the content returned.

"\w" represents a match from small to Z, uppercase A to Z, and the number 0 to 9 contains the preceding three, as printed above.

"\w" in the regular inside represents a match except the letter and the number of special symbols, but here the use of the slash to note in the string \ is the escape symbol specific Baidu to learn.

Here the parentheses () are used to indicate that the match is to take the contents of the parentheses inside, here. * is a regular greedy matching syntax hundred points is greedy benefits maximum range matching criteria as shown above.

Here's a question mark. *? is to limit it so that his maximum range of matches is also called a non greedy pattern match. The result is that the content within the two Div is returned.

Here add Re. I (uppercase I) indicates a match regardless of the male's mother's case is all take all, otherwise the upper case will appear the above match cannot find return empty list to you.

Here's another thing is \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ n S (uppercase) This represents the return of all character contents, including the line-wrapping, basic you put the above grammar and usage after the basic 70% matching methods are all done, of course, there is a very good way I do not enumerate, we can learn by themselves (the rest of the basic I have rarely used it).

The usage and difference of 2.match and search:

Re.match attempts to match a pattern from the starting position of the string, and match () returns none if the starting position match succeeds. Re.search scans the entire string and returns the first successful match. Look at the code and it's easy to understand. As follows:

Here the direct print end plus. span () can get the position of the matching string in tuple tuple return (starting position, end position), one not written, because he returned null plus the compiler will error.

is not at a glance, match will only start match, can not find the return of none, here I did not add. Group () is because the return value is null value I added the compiler will error, search is not picky about scanning the entire string, of course, can also use the above regular method to match, Here is not too much to introduce everyone can practice.

Usage of 3.sub substitution

Sub is used to replace a match in a string, and the syntax is generally re.sub (r ' regular match rule ', ' replacement string ', string to retrieve)

Here is a very intuitive response to the result, the # and the following string to replace the string you want to change.

4. Final Benefits

Before giving the final benefits, I hope that we can practice the above usage and use rules, only a lot of mistakes will accumulate experience, the last benefit tell you several common mailbox matching rules are as follows:

Must kill skills finally sent a fit a variety of matching or hair | Used to match a number of different mailbox use, everyone as long as the above method of conservative more than 70% of the place can be used to, the final statement is only the younger brother to share the personal understanding, the big guys ignore it, thank you, finally still old lines: Thank you, see you next time!

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.