This example describes the Python regular expression match and search usages. Share to everyone for your reference. The specific analysis is as follows:
Python provides the main regular expression operations in 2: Re.match and Re.search.
Match: Matches only from the beginning of the string to the regular expression, the match returns Matchobject successfully, otherwise none is returned;
Search: The string attempt to match all strings to the regular expression, if all of the characters are not successfully matched, return none, otherwise return matchobject; (re.search equivalent to the default behavior in Perl)
Import Redef Testsearchandmatch (): s1= "HelloWorld, I am!" 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
Hopefully this article will help you with Python programming.