Python-re Module

Source: Internet
Author: User

module RE
[Character Group]
Indicates that the various characters that may appear in the same position form a group of characters, expressed in a regular expression using []
Characters are divided into classes, such as numbers, letters, punctuation, etc.
A string that represents a number:
Abbreviated mode must be from small to large
Group of characters representing letters
[ABCD]
[A-d]
means match any character [\w\w][\s\s][\d\d]

The common methods under the RE module are:

ImportReret= Re.findall ('a','Eva Egon Yuan')#returns all results that meet the matching criteria, placed in the listPrint(ret)#result: [' A ', ' a ']ret= Re.search ('a','Eva Egon Yuan'). Group ()Print(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 ()#same as search, but match at the beginning of the stringPrint(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) 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

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.