#/usr/bin/python#coding =utf-8# @Time: 2017/11/18 18:24# @Auther: Liuzhenchuan# @File: Re's split FindAll Finditer method. pyImport re #re. Compile to compile a regular expression into an object#split () method, is the splitp = re.compile (R ' \d+ ') a_str = ' One1two2three3foure4 ' #把p的正则当成分隔符, The string is cut with p and finally returned to print # # # ' *5 + " separated by digital \d ' *5 print p.split (' One1two2three3foure4 ') #使用正则匹配分隔字符串print p.split (a_str) print ' # # # '* + '\ n '#以空白字符进行分隔print # # # ' *5 + " separated by a blank character \s ' + # # # ' *5 m = re.compile (R ' \s+ ') print m.split (' 123 456 7890 890 ') # #以非单词进行分隔print # # # ' *5 + " separated by a non-word string ' *5 n = re.compile (R ' \w+ ') print n.split (' 1234**4567p890**op ') print ' # # # '* + '\ n '# #正则对象findall () to find a string that conforms to the object. Return as a listPrint ' find compliant with pure numbers ' p = re.compile (R ' \d+ ') a_str = ' One1two2three3foure4 ' print p.findall (a_str) print ' # # # '*5 + '\ n' print ' Finditer () method ' #finditer () method, finditer an iterative object, the iterative method is better than the FindAll methodfor i in p.finditer (a_str): Print i.group () >>>############## #以数字 \d to separate ###############[' One ', ' One ', ' three ', ' foure ', '][' One ', ' One ', ' three ', ' foure ', ']######################################################################################################## #以空白字符 \s to separate ###############[' 123 ', ' 456 ', ' 7890 ', ' 890 ']############## #以非单词串进行分隔 ###############[' 1234 ', ' 4567p890 ', ' op ']##########################################################################################to find a pure digit-compliant[' 1 ', ' 2 ', ' 3 ', ' 4 ']###############Finditer () method1234
Python Foundation 8.4 re Spilt () FindAll () Finditer () method