The difference between Re.search and Re.findall in Python

Source: Internet
Author: User

Reference Blog: http://www.crifan.com/python_re_search_vs_re_findall/

Here, I'd like to talk about the problems I'm having right now.

This is a local file text.txt

When I use Re.findall () to get its textual content,
<li><a href= "http://jikexueyuan.com/1.html" > This is the first article </a></li>
In the content of the <a> tag directly, using the following code, using the first hit after the small principle:
<pre name= "code" class= "python" >text_field = Re.findall (' <ul> (. *?) </ul> ', Html,re. S
Text = Re.findall (' "> (. *?) </a></li> ', Text_field,re. S) for each of the Text:print each

It should be fine, but it's a mistake.

There was no solution, and then I tried.

1.

Text_field = Re.findall (' <ul> (. *?) </ul> ', Html,re. S) [0]
2.
Text_field = Re.search (' <ul> (. *?) </ul> ', Html,re. S). Group (1) <pre style= "Font-family:consolas; font-size:10.5pt; Background-color:rgb (255, 255, 255); " > Both of these methods are correct

But I don't understand. I had the same feeling when I saw the last mention of the blog at the beginning. At this point, I suspect that when FindAll gets an element that is a tuple, but this tuple has only one, it needs to be given a location like search.

But now I don't know exactly what the situation is to write like this. I hope that the great God will not hesitate to enlighten!


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The difference between Re.search and Re.findall in Python

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.