Python common modules (iv)

Source: Internet
Author: User

One. Re module

Regular expressions when a concept of computer science, regular expressions are often used to retrieve, replace those that conform to a pattern of text, most programming languages support the use of regular expressions for string manipulation.

A regular is a method of combining symbols with special meanings to describe a character or string, or a rule that is used to describe a class of things. It is embedded in Python and is implemented through the RE module, and the regular expression pattern is compiled into a series of byte codes, The matching engine written by C is then executed. The function of the RE module is to filter the string, and if you want to find what you want in a string, you need to tell the filter rule, which is the regular expression.

Common matching patterns:

  

ImportRe#Pending StringSTR1 ='abc-d$eaf+ \ 1*/__g12a3'#FindAll () finds all the conditions in the string that satisfy the#\w Find alphanumeric underlines \w find non-alphanumeric underscoresPrint(Re.findall ('\w', str1))Print(Re.findall ('\w', str1))#\s Find all invisible characters \s find all visible charactersPrint(Re.findall ('\s', str1))Print(Re.findall ('\s', str1))#\d Find any number \d find any non-digitalPrint(Re.findall ('\d', str1))Print(Re.findall ('\d', str1))#\ nPrint(Re.findall ('\ n', str1))#. Matches any character except a line breakPrint(Re.findall ('.', str1))#\s\w\d are all matched to a single character#Match repeating characters * +? {}#* Previous expression matches 0 or more timesPrint(Re.findall ('\w\d*', str1))#+ Previous expression matches 1 or more timesPrint(Re.findall ('\d+','1 asf 2'))#The preceding expression matches 1 or 0 timesPrint(Re.findall ('\d?','1 111'))#{m,n} at least m times, up to N timesPrint(Re.findall ('\d{1,3}','1 12111'))#{m} must be M-timesPrint(Re.findall ('[A-z]{3}','AAA AA a AA AAA AAAA'))#find the left and right 0 or 1 or 2 from the character#| Match RangePrint(Re.findall ('0|1|2','123af45ad60d21'))#[] Character set in parentheses the symbol is not whole is a single characterPrint(Re.findall ('[012]','123af45ad60d21'))#use the caret to indicate the inverse of a range matchPrint(Re.findall ('[^0-9]','123af45ad60d21'))#Please find out all the numbers 0-9 and the letters A-Z A-a A-a a a A-minus only two characters in the middle of the meaning of rangePrint(Re.findall ('[0-9a-za-z]','123+_lk# $a'))#^ Match beginning of linePrint(Re.findall ('^h','hellohh'))#$ match Line tail note $ write after expressionPrint(Re.findall ('ha$','Hellohha'))#\b Matches the end of a wordPrint(Re.findall (R'h\b','Elloh Wohrld Okhi'))#Greedy match * +#will match until the condition is not met. To stop the greedy match (matches the minimum number of characters that satisfy the condition)Print(Re.findall ('\w*?','SFASDEFD')) src=""#() is used to group regular expressions (group) without altering the original expression logical meaning#Take precedence over what's in parentheses? : Cancels the precedence of parenthesesPrint(Re.findall ("src= ' (. +?) '", SRC))
Two. subprocess Module

The Subprocess module is a new module in python2.4 that allows you to generate new processes, connect to their in/out/err pipelines, and get their return codes.

Common functions in the subprocess module:

Subprocess.run () The new function in Python 3.5. Executes the specified command, and then returns an instance of the Completedprocess class that contains the execution result after the command execution is completed

Subprocess.call () Executes the specified command, returning the command execution state, which functions like Os.system (CMD).

Subprocess. Popen () This class is used to execute a subroutine in a new program. The above functions are implemented based on the Subprocess.popen class.

Instance code:

ImportSubprocessres= Subprocess.run ('tasklist', shell=true,stdout=subprocess. PIPE)Print(Res.stdout.decode ('GBK'))Print(Res.stderr)#res = Subprocess.call ('tasklist', shell=True)Print(RES)#The first process a reads the contents of tasklist and hands the data to another process B another process B writes the data to a fileRes1 = subprocess. Popen ('tasklist', Stdout=subprocess. Pipe,shell=true,stderr=subprocess. PIPE) Res2= subprocess. Popen ('findstr cmd', Stdout=subprocess. Pipe,shell=true,stderr=subprocess. pipe,stdin=res1.stdout)Print(Res2.stdout.read (). Decode ('GBK'))

Python common modules (iv)

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.