Python regular expression match and search usage instance, pythonmatch
This article describes the usage of the python regular expression match and search. Share it with you for your reference. The specific analysis is as follows:
Python provides two major regular expression operations: re. match and re. search.
Match: match the regular expression only from the beginning of the string. If the match succeeds, matchobject is returned; otherwise, none is returned;
Search: match all strings in the string with the regular expression. If none of the strings are matched successfully, none is returned. Otherwise, matchobject is returned. (re. search is equivalent to the default behavior in perl)
Import redef testsearchandmatch (): s1 = "helloworld, I am 30! "W1 =" world "m1 = re. search (w1, s1) if m1: print ("find: % s" % m1.group () if re. match (w1, s1) = none: print ("cannot match") w2 = "helloworld" m2 = re. match (w2, s1) if m2: print ("match: % s" % m2.group () testsearchandmatch () # find: world # cannot match # match: helloworld
I hope this article will help you with Python programming.