The difference between search () and match () in Python

Source: Internet
Author: User

The match () function only detects if the beginning of the string matches, and the match succeeds to return the result, otherwise none

ImportRePrint(Re.match ("func","function"))#Print the result <_sre. Sre_match object; Span= (0, 4), match= ' func ' >Print(Re.match ("func","function"). span ())#printing Results (0, 4)Print(Re.match ("func1","function"))#Print result NoneNote:Print(Re.match ("func1","function"). span ()) will error because no span is taken

The search () function looks for pattern matching throughout the string, only to find the first match and then returns an object that contains matching information, which can get a matching string by calling the group () method, or None if the string does not match.

ImportRePrint(Re.search ("tion","function"))#Print the result <_sre. Sre_match object; Span= (4, 8), match= ' tion ' >Print(Re.search ("tion","function"). span ())#Printing Results (4, 8)Print(Re.search ("Tion1","function"))#Print result NoneNote:Print(Re.search ("Tion1","function"). span ()) will error because Tion1 is not being taken
Other common methods under the RE module
ImportRePrint(Re.findall ("a","a AA ab AC"))#returns all results that meet the matching criteria, placed in the list#[' A ', ' a ', ' a ', ' a ', ' a ']Print(Re.split ('[AB]','ABCD'))#first Press ' a ' to split the ' and ' BCD ', in the pair ' and ' BCD ' separate by ' B '#[', ', ', ' 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) 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 resultPrint([I.group () forIinchRET])#view remaining left and right results

Attention:

1 FindAll Priority query:

Import Reret = Re.findall (' www. ( BAIDU|JD). com ', ' www.jd.com ') print (ret)  # [' JD ']     this is because FindAll will prioritize the contents of the matching results group, if you want to match the result, cancel the permission can be ret = Re.findall (' www. (?: BAIDU|JD). com ', ' www.jd.com ') print (ret)  # [' Www.jd.com ']

2 Split's priority query

Ret=re.split ("\d+", "Eva3egon4yuan") print (ret) #结果: [' Eva ', ' Egon ', ' Yuan ']ret=re.split ("(\d+)", "Eva3egon4yuan") Print (ret) #结果: [' Eva ', ' 3 ', ' Egon ', ' 4 ', ' Yuan '] after #在匹配部分加上 () The result is different, the #没有 () does not retain the matching items, but there is () can retain the matching items, # This is very important in some of the use processes that need to keep the matching part.

The difference between search () and match () in Python

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.