Python Learning Notes (24) Regular

Source: Internet
Author: User

Vamei Source: Http://www.cnblogs.com/vamei Welcome reprint, Please also keep this statement. Thank you!

Original chain: http://www.cnblogs.com/vamei/archive/2012/08/31/2661870.html

NOTES: Half of the notes, dabbler levels

#The 25th is about re regular#Syntax:#Re.match (Pattern, String, flags=0)#two parameters, pattern matching regular expression str requires regular Match string flag case multi-line matchImportREM= Re.search ('[0-9]','Fdaf6dfae')Print(M.group (0))Print(M.group ())#return is the number matched to" "about group not introducing group in our match, the whole expression is a group, the group () with group (0), so the output is consistent so print has M.group (0)" "a='13abc456'Print(Re.search ('([0-9]*) ([a-z]*) ([ 0-9]*)', a). Group (0))#back to group (0) 132abc456Print(Re.search ('([0-9]*) ([a-z]*) ([ 0-9]*)', a). Group (1))#returns Group (1) The first brace matching partPrint(Re.search ('([0-9]*) ([a-z]*) ([ 0-9]*)', a). Group (2))#returns the group (2) Second Bracket matching sectionPrint(Re.search ('([0-9]) * ([A-z]) * ([0-9]*)', a). Group (3))#return Group (3) The third Bracket matching section" "1. The three sets of parentheses in the regular expression divide the matching result into three groups () the same group (0) is the match regular expression overall result group (1) lists the first brace matching part, Group (2) lists the second bracket matching part, Group (3) lists the third The matching section of the parentheses. 2. No match succeeded, Re.search () returns NONE3. Of course, there is no parentheses in the expression, group (1) must be wrong, indexerror,no such group does not have this group" "#functions of regular Expressions#m = Re.search (PATTERN,STR) #搜索整个字符串 until a matching string is found#m = Re.match (PATTERN,STR) #从头检查str是否符合正则表达式, must match from first characterm= Re.search ('[0-9]','Fdaf6dfae')#if it matches with the match function, the return is none.#string is the beginning of F, does not conform to ' ([0-9]) ' Requirements#replace the regular matched character#str = re.sub (pattern, replacement, str)a= Re.sub ('([0-9])','6','ADF3DSF')Print(a)#3 is replaced with 6 after the output.#The regular matched characters are slicedA = Re.split ('([0-9])','FAFFD3FDF')Print(a)#after the output, the entire str is sliced into 3 slices, the first half character, the number, the second half character, and the list returns#regular Match search stringA = Re.findall ('a','Fdsagr')Print(a)#The output is to put the matching string in the list to return#the author finally notes Re.compile, let's get to know#I found the information, I can't read it.#compiles a regular expression pattern that returns the schema of an object. (You can compile common regular expressions into regular expression objects, which can be a bit more efficient.) )#syntax: Re.compile (pattern,flags=0)ImportRett="Tina is a good girl, she's cool, clever, and so on ..."RR= Re.compile (r'\w*oo\w*')Print(Rr.findall (TT))#Find all words that contain ' oo '#\w Match word * matches one character 0 or unlimited (repeats >=0 times)#R is to prevent escape from preserving the original style

Python Learning Notes (24) Regular

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.