The first regular expression of Python
R ' Imooc ' Pattern Match result
In [2]:ImportReIn [3]: PA = re.compile (r'IMOOC')#the compile method of re generates pattern objectIn [4]: Type (PA) out[4]: _sre. Sre_patternin [5]: Pa.pa.findall pa.fullmatch pa.match pa.search pa.subnpa.finditer pa.groupindex Pa.pattern Pa.split pa.flags pa.groups pa.scanner pa.sub in [8]: str ='Imooc python'In [9]: Pa.match (str)#Match method of the Pattern object matches the stringOUT[9]: <_sre. Sre_match object; Span= (0, 5), match='IMOOC'>In []: MA =Pa.match (str) in [11]: MA. Ma.end ma.group ma.lastgroup ma.re ma.start ma.endpos ma.groupdict ma.lastindex ma.regs ma.string ma.expand ma.groups MA.P Os Ma.span in [11]: Ma.group () out[11]:'IMOOC' #returns the string that matches toIn [15]: Ma.span () out[15]: (0, 5)#returns the matched string subscript index
In [1]:ImportReIn [2]: PA = re.compile (r'IMOOC', Re. I)#Ignore letter CaseIn [3]: paout[3]: Re.compile (r'IMOOC', Re. ignorecase|Re. UNICODE) in [4]: ma = Pa.match ('Imooc python') in [5]: Ma.group () out[4]['IMOOC'In [6]: Ma = Pa.match ('Imooc python') in [7]: Ma.group () out[7]:'IMOOC'In [8]: ma = Pa.match ('Imooc python') in [9]: Ma.group () out[9]:'IMOOC'In [10]: ma.groups () out[10]: () in [ALL]: PA = re.compile (r'(IMOOC)', Re. I) in [[]: Ma = Pa.match ('IMOOC') in [13]: Ma.group () out[13]:'IMOOC'In [14]: ma.groups () out[14]: ('IMOOC',)#return as a groupIn [[]: Ma = Re.match (r'IMOOC','Imooc python')#compile and match can be mergedIn [16]: Ma.group () out[16]:'IMOOC'
Python regular expression re-module uses