This example describes Python's re module application. is a very important application technique. Share to everyone for your reference.
The specific methods are as follows:
Import Re # match_object = Re.match (' foo ', ' foo ') if Match_object is not none:print type (match_object) print mat Ch_object.group () # match_object = Re.match (' foo ', ' FOOABV ') if Match_object not None:print match_object.group (
#match从头开始匹配 match_object = re.match (' foo ', ' AFOOABV ') if Match_object is not none:print match_object.group () Else:print ' not match ' #利用面向对象的特点, one row completes the print Re.match (' Love ', ' lovesomebody is a happy thing '). Group () #与mat The difference between CH: match starts from scratch, search is lookup match_object = re.search (' foo ', ' AFOOABV ') if Match_object is not none:print match_object . Group () Else:print ' not match ' #| use BT = ' Bat|bit|bot ' match_object = Re.match (BT, ' BATSDF ') if Match_object Is isn't None:print "|...|" + match_object.group () #会匹配成功 else:print ' not match ' BT = ' Bat|bit|bot ' match_obj ECT = Re.search (BT, ' AABATSDF ') if Match_object is not None:print "|search|" + match_object.group () #会匹配成功, if you use match With success Else:print ' not Match
This article example test environment for Python2.7.6
The results of the operation are as follows:
<type ' _sre. Sre_match ' >
foo foo not
Match love
foo
|...| Bat
|search|bat
I hope this article will help you with your Python programming.