python--Regular Match_compile_search_findall usage

Source: Internet
Author: User

Regular expressions are powerful, but it takes a bit of time to learn to be proficient.

The following is a general approach to match,compile,search, andFindAll

    • Match

starts from the first character of the string if it does not match to the return None, match to return an object

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M01/9B/CD/wKiom1lnNMOR_QfTAAARIhJPi0A364.png-wh_500x0-wm_ 3-wmp_4-s_4217727800.png "title=" match does not match to. png "alt=" wkiom1lnnmor_qftaaarihjpi0a364.png-wh_50 "/>

not matched to return None

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M00/9B/CC/wKioL1lnNM2DABSvAAAQmmjmux4207.png-wh_500x0-wm_ 3-wmp_4-s_397505854.png "title=" matches to. png "alt=" wkiol1lnnm2dabsvaaaqmmjmux4207.png-wh_50 "/>

The start character matches to H, returns an object, and needs to get the H value through group .

    • Search

Search is similar to match , except that the entire string is searched and the first match to the specified character returns a value that does not match to None. Get a worthwhile method also need to pass Group ()

650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M00/9B/CD/wKiom1lnNNjir83pAAANKxpCEag476.png-wh_500x0-wm_ 3-wmp_4-s_1787002632.png "title=" search does not match to. png "alt=" wkiom1lnnnjir83paaankxpceag476.png-wh_50 "/>

no match to character returns None

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M00/9B/CD/wKiom1lnNOSSHlDJAAAQ7A9SHZg008.png-wh_500x0-wm_ 3-wmp_4-s_867038957.png "title=" search matches to. png "alt=" wkiom1lnnosshldjaaaq7a9shzg008.png-wh_50 "/>

matches from the beginning of the string, and returns an object if one matches. You need to get the matching value through group.

    • Findall

Findall matches all values in the string that are related to the specified value and is returned as a list. An empty list is returned if it is not matched to.

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M00/9B/CD/wKiom1lnNO_jsXynAAAK4RQgyw0439.png-wh_500x0-wm_ 3-wmp_4-s_781194583.png "title=" FindAll does not match to. png "alt=" wkiom1lnno_jsxynaaak4rqgyw0439.png-wh_50 "/>

does not match to return an empty list.

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M01/9B/CD/wKiom1lnNPmQvO12AAALDTiG3KE923.png-wh_500x0-wm_ 3-wmp_4-s_1557466438.png "title=" FindAll matches to. png "alt=" wkiom1lnnpmqvo12aaaldtig3ke923.png-wh_50 "/>

Matches all values of the specified character and returns the value as a list.

    • Compile

Re.compile is to convert a regular expression to a pattern object, which makes it more efficient to match. after you use compile to convert once, you do not need to convert each time you use the mode

650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M01/9B/CD/wKioL1lnNQbSp5moAAARzTNHo3I065.png-wh_500x0-wm_ 3-wmp_4-s_3098510934.png "title=" compile convert. png "alt=" wkiol1lnnqbsp5moaaarztnho3i065.png-wh_50 "/>

In fact, this is the same as executing re.findall (' img ', a), but after using compile, there is no need to convert the schema object once the FindAll method is executed.


This article is from the "It Bug" blog, so be sure to keep this source http://laomomo.blog.51cto.com/6595318/1947243

python--Regular Match_compile_search_findall usage

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.