Python Regular Expression re findall

Source: Internet
Author: User

The Python Regular Expression re findall can return matched substrings in the form of a list.

Re. findall (pattern, string [, flags]):
Search for strings and return all matching substrings in the form of a list. First look at a simpleCode:

Import re
 
P = Re. Compile (R' \ D + ')
Print P. findall ('one1two2three3four4 ')
 
### Output ###
# ['1', '2', '3', '4']

A little complex, such:
Info = '<a href = "http://www.baidu.com"> Baidu </a>' Our requirement is to use regular expressions to extract the URL and the anchor text, which can be used
Findall ()

Import Re
Relink = '<A href = "(. *)"> (. *) </a>'
Info = '<A href = "http://www.baidu.com"> Baidu </a>'
Cinfo = Re . Findall ( Relink , Info )
Print Cinfo

Output result: [('HTTP: // www.baidu.com ', 'baidu')] returns a list of tuples formed by matching results. If you need to replace the regular expression, you can check Python RE sub.

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.