Python re module

Source: Internet
Author: User

Re module common methods:

ImportReret= Re.findall ('a','Eva Egon Yuan')#returns all results that meet the matching criteria, placed in the list,#Re.findall (' Regular expression ', ' Match string ') returns the string that satisfies the expression in the string to be matchedPrint(ret)#result: [' A ', ' a ']ret= Re.search ('a','Eva Egon Yuan'). Group ()#The search method finds the first string within a string that satisfies the expression and returns a regular object address#to view the returned content through group (), no match succeeded String returned nonePrint(ret)#result: ' A '#The function finds a pattern match within the string, only to find the first match and then returns an object that contains the matching information that can be#a matching string is obtained by calling the group () method, and none is returned if the string does not match. ret= Re.match ('a','ABC'). Group ()#with search, but only at the beginning of the string, start out a string that does not match the expression, return none,#If the match succeeds, the returned string is viewed by group ()Print(ret)#result: ' A 'ret= Re.split ('[AB]','ABCD')#first Press ' a ' to split the ' and ' BCD ', in the pair ' and ' BCD ' separate by ' B 'Print(ret)#[', ', ', ' CD ']ret= Re.sub ('\d','H','Eva3egon4yuan4', 1)#replace number with ' H ', parameter 1 means replace only 1Print(ret)#Evahegon4yuan4ret= Re.subn ('\d','H','Eva3egon4yuan4')#replace the number with ' H ', return the tuple (replace the result, replace the number of times)Print(ret)#results: (' Evahegonhyuanh ', 3)obj= Re.compile ('\d{3}')#compiles a regular expression into a regular expression object, with the rule matching 3 digitsret = Obj.search ('abc123eeee')#The regular Expression object calls search, and the argument is the string to be matchedPrint(Ret.group ())#results: 123ImportReret= Re.finditer ('\d','ds3sy4784a')#Finditer Returns an iterator that holds the matching resultPrint(ret)#<callable_iterator object at 0x10195f940>Print(Next (Ret). Group ())#View the first resultPrint(Next (Ret). Group ())#View the second result#for I in RET: #可以对迭代器进行迭代, view each content#print (I.group ())Print([I.group () forIinchRET])#view remaining left and right results

Attention:

1. FindAll's priority query: '?: ' returns the match for cancellation priority

ImportReret= Re.findall ('www. (baidu|oldboy). com','www.oldboy.com')Print(ret)#[' Oldboy '] This is because FindAll will prioritize the contents of the matching results group, and if you want to match the results, cancel the permissionsret= Re.findall ('www. (?: baidu|oldboy). com','www.oldboy.com')Print(ret)#[' www.oldboy.com ']

2. Split's priority query:

Ret=re.split ("\d+","Eva3egon4yuan")Print(ret)#results: [' Eva ', ' Egon ', ' Yuan ']ret=re.split ("(\d+)","Eva3egon4yuan")Print(ret)#results: [' Eva ', ' 3 ', ' Egon ', ' 4 ', ' Yuan ']#after the matching section plus () The result is different,#There is no reservation for the matching item, but there is a () to keep the matching item .#This is very important in some of the use processes that need to keep the matching part. 

Cases:

Group naming:

(? p<name> regular expression), indicating the name of the group

(? P = name) indicates the use of this grouping, where the match should be exactly the same as the content in the group

Python re module

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.