Python Regular Expressions

Source: Internet
Author: User

Python uses the RE module to provide the ability to handle regular expressions

Constant

常量                                     说明re.M、re.MULTILINE          多行模式  re.S、re.DOTALL                单行模式re.I、re.IGNORECASE       忽略大小写re.X、re.VERBOSE            忽略表达式中的空白字符使用 | 位或 运算开启多种选项
Method

Compile

re.compile(pattern,flags =0)设定flags,编译模式,返回正则表达式对象regex。pattern就是正则表达式字符串,flags是选项。正则表达式需要被编译,为了提高效率,这些被编译后的结果被保存,下次使用同样的pattern的时候,就不需要再次编译。re的其他方法为了提高效率都调用了编译方法,就是为了提速

One-time match

Re.match (pattern,string,flags=0) Regex.match (String[,pos[,endpos]) match match matches from the beginning of the string, The Regex object Match method can reset the starting position and end position, returning the match object Re.search (pattern,string,flags=0) Regex.search (String[,pos[,endpos]) Searching from scratch until the first match, the Regex object Search method can reset the start and end positions, returning the match object Re.fullmatch (pattern,string,flags=0) REGEX.FULLMATC (string[, Pos[,endpos]]) the entire string and regular expression matches the import re s = ' bottle\nbag\nbig\napple ' for i,c in Enumerate (s,1): Print ((i-1,c), end  = ' \ n ' if i%8==0 Else ') print () (0, ' B ') (1, ' O ') (2, ' t ') (3, ' t ') (4, ' L ') (5, ' E ') (6, ' \ n ') (7, ' B ') (8, ' a ') (9, ' G ') (Ten, ' \ n ') (One, ' B ') (The ' I ') (The ' G ') (' \ n ') (In ' a ') (+, ' P ') (+, ' P ') (at ' l ') (d, ' e ') #match method print ('--match--') result = Re.match (' B ', s) # find one without looking for print (1,result) result = Re.match (' A ', s) print (2, Result) #没找到返回Noneresult = Re.match (' ^a ', s,re. M) # Still looking from the beginning, multiline mode does not use print (3,result) result = Re.match (' ^a ', s,re. S) #依然从头开始找print (4,result) #先编译, and then use regular expressions to express regex = Re.compile (' a ') result = Regex.match (s) #依然从头开始找print (5,result)  result = Regex.match (s,15)# put index 15 as start looking for print (6,result) print () Search method print ('--search--') result = Re.search (' A ', s) # Scan to find the first position matching print (7,result Regex = Re.compile (' b ') result = Regex.search (s,1) print (8,result) # Bagregex = Re.compile (' ^b ', re. M) result = Regex.search (s) # no matter if it is not multiple lines, find it back to print (8.5,result) #bootleresult = Regex.search (s,8) print (9,result) # Bigfullmatch method result = Re.fullmatch (' bag ', s) print (10,result) regex = re.compile (' bag ') result = Regex.fullmatch (s) Print (11,result) result = Regex.fullmatch (s,7) print (12,result) result = Regex.fullmatch (s,7,10) print (13,result) # To exactly match, the less is not enough, [7,10) Full-text method Re.findall (pattern,string,flags=0) Regex.findall (String[,pos[,endpos]]) to the entire string, from left to right, Returns a list of all occurrences Re.finditer (pattern,string,flags=0) regex.fingiter (String[,pos[,endpos]) that matches the entire string, from left to right, and returns all occurrences. Returns an iterator note that each iteration returns a match object. Import re s = ' bottle\nbag\nbig\nable ' for i,c in Enumerate (s,1): Print ((i-1,c), end = ' \ n ' if i%8==0 Else ') pr Int () (0, ' B ') (1, ' O ') (2, ' t ') (3, ' t ') (4, ' L ') (5, ' E ') (6, ' \ n ') (7, ' B ') (8, ' a ') (9, ' G ') (, ' \ n ') (one, ' B ') (A, ' I ') (A, ' I ') (+, ' \ n ') (+, ' a ') (+, ' B ') (+, ' l ') (+, ' e ') FindAll method result = Re.findall (' b , ', s) print (1,result) regex = Re.compile (' ^b ') result = Regex.findall (s) print (2,result) regex = Re.compile (' ^b ', re. M) result = Regex.findall (s,7) print (3,result) #bag Bigregex = Re.compile (' ^b ', re. s) result = Regex.findall (s) print (4,result) # Bottleregex = Re.compile (' ^b ', re. M) result = Regex.findall (s,7,10) print (5,result) # Bagfiditer Method result = Regex.finditer (s) print (type result) print ( Next (Result) print (next result)

match Substitution

Re.sub (pattern,replacement,string,count=0,flags=0) regex.sub (replacement,string,count=0) uses pattern to match string strings , use repl substitution for matches. The replacement can be string, Bytes, functionre.subn (pattern,replace,string,count=0,flags=0) regex.subn (replance,string, string,count=0) with sub returns a tuple (New_string,number_of_subs_made) import re s = "' Bottle\nbag\nbig\napple" ' for i,c in  Enumerate (s,1): Print ((i-1,c), end = ' \ n ' if i%8==0 Else ') print () (0, ' B ') (1, ' O ') (2, ' t ') (3, ' t ') (4, ' L ') (5,  ' E ') (6, ' \ n ') (7, ' B ') (8, ' a ') (9, ' G ') (ten, ' \ n ') (one, ' B ') (A, ' I ') (+, ' g ') (+, ' \ n ') (+, ' a ') (+, ' P ') (+, ' P ') (at ' l ') (+, ' e ') Replacement method regex = Re.compile (' b\wg ') result = Regex.sub (' magedu ', s) print (1,result) result = Regex.sub (' magedu ', s,1) Print (2,result) regex = Re.compile (' \s+ ') result = Regex.subn (' \ t ', s) print (3,result) # The substituted string and the number of substitutions of the tuple split string Re.split ( pattren,string,maxsplit,flags=0) Re.split (split string) Import res = "Bottle02 bag03 big1100 Able" "for i,c in enumerate (s,1): Print ((i-1,c), end= ' \ n ' if i%8==0 Else ') print () extracts each line of words out of print (S.split ()) #做不到 [' big1 ' ...] result = Re.split (' [\s\d]+ ', s) print (1,result) regex = Re.compile (' ^[\s\d]+ ') result = Regex.Split (s) print (2,result) Regex = Re.compile (' ^[\s\d]+ ', re. M) result = Regex.Split (s) print (3,result) regex = Re.compile (' \s\d+\s+ ') result = Regex.Split (' +s ') print (4,result)

Grouping

The data captured using the parentheses pattern is placed in the group. The match, search function returns a Match object, FindAll returns a list of strings, Finditer returns a match object if the pattern uses a grouping, if the match has a result, it will be in the Match object 1, using Group (N) The method returns the corresponding grouping, 1 to n is the corresponding grouping, 0 returns the entire match character 2, if a named grouping is used, you can use group (' name ') to take the group 3, use Groupdict () to return all named groupings import re s = "' Bottle\nbag \nbig\napple "for I,c in Enumerate (s,1): Print ((i-1,c), end = ' \ n ' if i%8==0 Else ') print () #分组regex = Re.compile ( ' (b\w+) ') result = Regex.match (s) print (type (result)) print (1, ' Match ', result.groups ()) result = Regex.search (s,8) print (2, ' Match ', Result.groups ()) #命名分组regex = Re.compile (' (b\w+) \ n (? p<name2>b\w+) \ n (? p<name3>b\w+) ' result = Regex.match (s) print (3, ' match ', result) print (4,result.group (3), Result.group (2), Result.group (1)) print (5,result.group (0). Encode ()) # 0 returns the entire matching string, which is Matchprint (6,result.group (' name2 '), Result.group ( ' Name3 ') print (7,result.groups ()) print (8,result.groupdict ()) result = Regex.findall (s) for x in result: # string list Prin T (type (x), x) regex = Re.compile (? p

Python Regular expression

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.