How to use Python regular expression match,search,find

Source: Internet
Author: User

1. Match the string using match ():

The match () function attempts to match the pattern from the beginning of the string ,

The group () method of the matching object can be used to display that successful match.

>>>ImportRe>>>m=re.match ('Foo','fooid'). Group ()'Foo'>>>n=re.match ('Foo','idfooid'). Group ()attributeerror:'Nonetype'object has no attribute'Group'

2. Use the search () function to match a string

Search () works exactly like match (), except that search () uses its string parameters,

Searches for the first occurrence of a given regular expression pattern at any location.

If the match succeeds, a matching object is returned.

>>>import  re>>>m=re.search ('foo' ) Idfoo'). Group ()>>>m'foo'

How to use Python regular expression match,search,find

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: info-contact@alibabacloud.com 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.