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.