Python regular expression re-module other methods
1:search (pattern,string,flags=0)
Find a match in a string
2:findall (pattern,string,flags=0)
Find a match, return the list of all matching parts
In [1]:ImportReIn [2]: str1 ='Imoooc videonum ='In [3]: Str1.find (' +') out[3]: 18In [4]: info = re.search (r'\d+', STR1) in [5]: infoout[5]: <_sre. Sre_match object; Span= (+), match=' +'>In [6]: Info.grinfo.group info.groupdict info.groups in [6]: Info.group () out[6]:' +'In [7]: str1 ='Imoooc videonum = 10000'In [8]: info = re.search (r'\d+', STR1) in [9]: infoout[9]: <_sre. Sre_match object; Span= (at $), match='10000'>In [10]: Info.group () out[10]:'10000'In [ALL]: str2 ='c++=100, java=90, python=80'In [[]: info = re.search (r'\d+', STR2) in [13]: infoout[]: <_sre. Sre_match object; Span= (4, 7), match=' -'>In [14]: Info.group () out[14]:' -'In [[]: info =re.findre.findall re.finditer in [[]: info = Re.findall (r'\d+', STR2) in [16]: infoout[16]: [' -',' -',' the']in [+]: sum ([Int (x) forXinchInfo]) out[17]: 270
3.sub (pattern,repl,string,count=0,flags=0)
Replace the part of a string that matches a regular expression with another value
4.split (pattern,string,maxsplit=0,flags=0)
Returns a list of split strings, based on the matching split string
In []: STR3 ='Imooc videonum ='In []: info = re.sub (r'\d+','1001', STR3) in [25]: infoout[25]:'Imooc videonum = 1001'In [26]:defadd1 (Match): .....: Val=Match.group () ....: Num= Int (val) +1 ....: returnstr (num) ....: in [27]: str3out[27]:'Imooc videonum ='In []: Re.sub (r'\d+', ADD1,STR3) out[28]:'Imooc videonum = 1001'
In [approx]: STR4 ='imooc:c C + + Java Python'In [37]: Re.sre.search re.sre_compile re.sub re.sysre.split re.sre_parse re.subn in [Notoginseng]: Re.split (r':| ', STR4) out[37]: ['IMOOC','C','C + +','Java','Python']in [[]: Re.split (r':| |,', STR4) out[38]: ['IMOOC','C','C + +','Java','Python']in [39]:
Introduction to the Re-module method of Python regular expressions