Python's re-module application example

Source: Internet
Author: User
This example describes the application of the RE module in Python. is a very important application skill. Share to everyone for your reference.

Here's how:

import re # match_object = Re.match (' foo ', ' foo ') if Match_object is not none:print type (match_ob ject) Print Match_object.group () # match_object = Re.match (' foo ', ' FOOABV ') if Match_object is not none:print match_o Bject.group () #match从头开始匹配 match_object = Re.match (' foo ', ' AFOOABV ') if Match_object is not none:print match_object. Group () Else:print ' not match ' #利用面向对象的特点, one line completes print re.match (' Love ', ' lovesomebody is a happy thing '). Group () #与mat CH difference: Match starts from scratch, search is find match_object = Re.search (' foo ', ' AFOOABV ') if Match_object is not none:print match_object.gr OUP () Else:print ' not match ' #| uses BT = ' Bat|bit|bot ' match_object = Re.match (BT, ' BATSDF ') if Match_object is not No Ne:print "|...|" + match_object.group () #会匹配成功 else:print ' not match ' BT = ' Bat|bit|bot ' Match_object = Re.search ( BT, ' AABATSDF ') if Match_object is not None:print "|search|" + match_object.group () #会匹配成功, if match will not be successful Else:print ' Not match ' 

The example test environment in this paper is Python2.7.6

The results of the operation are as follows:

 
  
   
  Foofoonot matchlovefoo|...| Bat|search|bat
 
  

Hopefully this article will help you with Python programming.

  • 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.