Python Basic---Common modules (not to be continued)

Source: Internet
Author: User

Re module (regular module)

A regular is a way to describe a character or string by combining symbols with special meanings, called regular expressions. Or, the regular is the rule used to describe a class of things. ( in Python ) it is embedded in python and is implemented through the RE module. The regular expression pattern is compiled into a sequence of bytecode, which is then executed by a matching engine written in C.

\w matching alphanumeric and underscore

\w match non-alphanumeric underline

\s matches any whitespace character, equivalent to "\t\n\r\f"

\s matches any non-null character

\d matches any number, equivalent to "0-9"

\d matches any non-numeric

\a Matching string

\z matches the end of the string, if there is a newline, matches only to the end string before the line break

\z Match string End

\g match the last match completed position

\ n matches a line break

\ t matches a tab

^ matches the beginning of the string

$ matches the end of a string

. matches any character, except the newline character, when re. When the Dotall tag is specified, it can match any character that includes a line feed

[...] used to represent a set of characters, listed separately: "amk" matches ' a ',' m ' or 'k'

[^ ...] not in the characters in []

* Match 0 or more expressions

+ match 1 or more expressions

? matches 0 or 1 fragments defined by a preceding regular expression, not greedy

{n} exact match n preceding expression

{N,m} matches n to m times the fragment defined by the preceding regular expression, greedy way

A|b match a or b

() matches the expression in parentheses, and also represents a group

Import reprint (Re.findall (' \w ', ' hello_ | egon 123 ')) print (Re.findall (' \w ', ' hello_ |  egon 123 ') Print (Re.findall (' \s ', ' hello_ | egon 123 \n \t ')) print ( Re.findall (' \s ', ' hello_ | egon 123 \n \t ')) print (Re.findall (' \d ', ' hello_ |  Egon 123 \n \t ') Print (Re.findall (' \d ', ' hello_ | egon 123 \n \t ')) Print (Re.findall (' h ', ' hello_ | hello h egon 123 \n \t ')) print (Re.findall (' \ Ahe ', ' hello_ | hello h egon 123 \n \t ')) print (Re.findall (' ^he ', ' Hello_  | hello h egon 123 \n \t ') Print (Re.findall (' 123\z ', ' hello_ |  Hello h egon 123 \n \t123 ') Print (Re.findall (' 123$ ', ' hello_ | hello h  egon 123 \n \t123 ') Print (Re.findall (' \ n ', ' Hello_ | hello h egon  123 \n \t123 ')) print (Re.findall (' \ t ', ' hello_ | hello h egon 123 \n \t123 ') output: [' h ',  ' e ',   ' l ',  ' l ',  ' o ',  ' _ ',  ' E ',  ' g ',  ' o ',  ' n ',  ' 1 ',  ' 2 ',  ' 3 '] ['   ',  ' | ',  '   ',  '   '] ['   ',  '   ',  '   ',  '   ',  ' \ n ',  '   ',  ' \ t '] [' H ',  ' e ',  ' l ',  ' l ',  ' o ',  ' _ ',  ' | ',  ' e ',  ' g ',  ' o ',  ' n ',   ' 1 ',  ' 2 ',  ' 3 ' [' 1 ',  ' 2 ',  ' 3 ' [' H ',  ' e ',  ' l ',  ' l ',  ' o ',  ' _ ',   '   ',  ' | ',  '   ',  ' e ',  ' g ',  ' o ',  ' n ',  '   ',  '   ',   ' \ n ',  '   ',  ' \ t ' [' H ',  ' h ',  ' h ' [' he '] [' he '] ' 123 ' [' 123 '] [' \ n '] [' t ']


the methods provided by the RE module are:

Re.findall () Find all results that meet the matching criteria and put them in the list

Re.search () finds only the first match and then returns an object that contains matching information, which can get a matching string by calling the group () method, and returns none if the string does not match
Re.match () with search, but at the start of a string match, you can use search+^ instead of match
Re.split () splits an object by matching content
Re.sub () Replace, (old value, new value, replace object, replacement number), do not specify the number of replacements, default replaces all
RE.SUBN () with sub, but returns the number of replacements in the result
Re.compile Reusing matching formats

3.Time Module

In Python, there are usually three ways to calculate the time:

A. Time stamp:

The timestamp represents the offset that is calculated in seconds, starting January 1, 1970 00:00:00. We run "type (Time.time ())" and return the float type

B. Formatted time string

C. Structured time

There are 9 elements in the Struct_time tuple: (year, month, day, time, minute, second, week of the year, Day of the Year, summer)

4.Random module

5.OS module

6.sys module

7.json and pickle modules

8.Shelve Module


This article is from the "Lyndon" blog, make sure to keep this source http://lyndon.blog.51cto.com/11474010/1955312

Python Basic---Common modules (not to be continued)

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.