Home >Backend Development >Python Tutorial >Python regular expression summary

Python regular expression summary

高洛峰
高洛峰Original
2017-03-08 11:08:481260browse

search(pattern, string, flags=0): Find a match in a string

finddall(pattern, string, flags=0): Find a match and return a list of all matching parts

sub(pattern, repl, string, count=0, flags=0): Replace the part of the string that matches the regular expression with other values ​​

split(pattern, string, maxsplit=0, flags=0): Split the string according to the match, Returns a list of split strings

import urllibimport re

req = urllib.urlopen('http://www.imooc.com/course/list')

buf = req.read()
listurl = re.findall(r'http:.+\.jpg', buf)print listurl

i = 0for url in listurl:
    f = open(str(i) + '.jpg', 'w')
    req = urllib.urlopen(url)
    buf = req.read()
    f.write(buf)
    i += 1


The above is the detailed content of Python regular expression summary. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn