This article mainly introduced the Python regular expression match and search usage, the example analyzed the regular expression in match and search function, the definition and the related use skill, has certain reference value, the need friend may refer to under
This example describes the Python regular expression match and search usage. 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 with the regular expression, the match returns Matchobject successfully, otherwise none is returned;
Search: All string attempts to match the regular expression, if all the strings are not matched successfully, return none, otherwise return matchobject; (re.search equivalent to the default behavior in Perl)
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16-17 |
Import re def 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 ("M Atch:%s "% M2.group ()) Testsearchandmatch () #find: World #cannot match #match: HelloWorld |
I hope this article will help you with your Python programming.