Python greedy, non-greedy, and python matching

Source: Internet
Author: User

Python greedy, non-greedy, and python matching

The python re module still needs to be re-learned.
The differences between python readline and readlines (), readline (), and read () have never been clear before. Now, I have figured out what is going on.
Readlines () can display the entire file together. This is also iterative display. You need to trace the rows and display the iterator pointer will be consumed.

In python, regular expressions match all characters other than line breaks .. * Cannot match all characters. In some cases, my conclusion is correct, but in some cases, it is wrong. The problem I encountered just now is finally known. It turned out to be a greedy match and a non-Greedy match problem. In this case, I am also relatively slow.
Save.html is an html that I can capture at will. I want to capture all javascript code segments from this html.

def getcss():    fh = open('save.html')    html =fh.read()    #js_pattern = re.compile(r'<script .*?">');    ans = re.findall(r'<script .*?>.*?</script>',html,re.S)    src_pattern = re.compile(r'^"http.*"');    for i in ans:        #ret = re.findall(src_pattern,i)        print i

Through the above program, I got the following results. The source file is an html file in a common format. These code segments belong to different branches. My goal is to find all the code segments. The following is a simple explanation. During the attempt, I tried several regular expressions. The following describes my problems.

Implementation is a cross-row match problem. * It cannot match \ n. So if tags are distributed in different rows, how can we match the intermediate content. To solve the problem of cross-row matching, you can find this solution using ([\ d \ D] *) or ([\ w \ W] *). or ([\ s \ S.
Then I encountered another problem. My regular expression always matches too much content. Finally, I found that my matching mode matches the most content. The default match mode is greedy match, so if. * is used, more content will always be matched. So I found this reference. What is the core of greedy match and non-Greedy match in python greedy match? The re. S mark is the key to multi-row matching. Similar tags include the re. M mark, which is the mark of a row. Re. m: ^ $ will match each line. By default, ^ and $ will only match the first line. There are several examples in this article, which are very good. It is worth studying carefully, we recommend that you execute these codes and check the results. If these codes are clear, the regular expressions are good.

re.findall(r"a(\d+?)", "a23b")re.findall(r"a(\d+)", "a23b")re.findall(r"a(\d+)b.+a(\d+)b", "a23b\na34b")re.findall(r"a(\d+)b.+a(\d+)b", "a23b\na34b", re.S)

Finally, I realized the above requirements, mainly using cross-row matching and non-Greedy matching.

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.