Introduction to the Re-module method of Python regular expressions

Source: Internet
Author: User

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

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.