Python Foundation 8.4 Re Spilt () FindAll () Finditer () method

Source: Internet
Author: User

#/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

Related Article

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.