Python greedy non-greedy match

Source: Internet
Author: User

The Python re module still needs to be re-learned.
The difference between Python ReadLine and ReadLines (), ReadLine (), and read () has never been clear before, and now it's clear what's going on.
ReadLines () can display the entire file together, this is also an iterative display, need to follow the line display, the iterator pointer will be consumed.

The regex in Python matches all characters except the newline: * It doesn't match all the characters. In some cases, my conclusion is correct, but in some cases my conclusion is wrong. Just met the problem, now finally know what is going on, the original is greedy match and non-greedy matching problem, for this situation, I am relatively dull.
Save.html was a random piece of HTML I grabbed, and I'm going to grab all the JavaScript snippets 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 result. The source file is an HTML, in general format, these snippets are branches, and my goal is to find out all the code snippets. Here's a simple explanation. I tried several regular expressions as I tried, and here are some of the problems I've encountered. The

Implementation is a cross-line matching issue. * is not matched \ n, so if the label is spread across different rows, then how to match the middle content. In order to solve the problem of cross-line matching, we found this solution, which can be replaced by ([\d\d]*) or ([\w\w]*) or ([\s\s]*)].
Then I get the problem, my regular matches too much, and I finally find that my matching pattern matches the most content. The default matching pattern is a greedy match, so if you use the. *, it will always match more content. So I found this reference. Python greedy match saw the core of the greedy match and the non-greedy match on one, re. The s tag is the key to multi-line matching, and a similar tag is the RE. M tag, this is a line of markup. Re. The m:^ $ flag will match each row, and the default ^ and $ will only match the first line, and there are a few examples in the article that are pretty good, worth examining carefully, suggesting that you execute the code yourself and see the results, if they are clear, Then the regular master is quite 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-line matching and non-greedy matching.

Python greedy non-greedy match

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.