Python regular expression match and search usage instance, pythonmatch

Source: Internet
Author: User

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" % () if re. match (w1, s1) = none: print ("cannot match") w2 = "helloworld" m2 = re. match (w2, s1) if m2: print ("match: % s" % () testsearchandmatch () # find: world # cannot match # match: helloworld

I hope this article will help you with Python programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.