Python Regular Expressions

Source: Internet
Author: User

The Python regular expression re FindAll method can return a matching substring in the form of a list.

Re.findall (pattern, string[, flags]):
Searches for a string, returning all matching substrings as a list. Let's look at a simple code:

Import re

p = re.compile (R ' \d+ ')
Print P.findall (' One1two2three3four4 ')

# # # output # #
# [' 1 ', ' 2 ', ' 3 ', ' 4 ']

Slightly more complex, such as:
info = ' <a href= ' http://www.baidu.com >baidu</a> ' Our need is to extract URLs and anchor text through regular expressions, 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

The result of the output: [(' http://www.baidu.com ', ' Baidu ')] Returns a list that contains the form of a tuple that matches the result. If you need to replace it with regular, you can look at the Python re Sub

Python Regular Expressions

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.