This article mainly introduces the usage of python regular expressions match and search. it analyzes the functions, definitions, and related usage skills of match and search in regular expressions, and has some reference value, for more information about how to use the python regular expression match and search, see the following example. 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.