题目网址http://www.pythonchallenge.com/pc/def/ocr.html
从网页源代码的指定的大段注释中找“rare character“。
import sys, urllib
import re
url = "http://www.pythonchallenge.com/pc/def/ocr.html"
wp = urllib.urlopen(url) #打开
content = wp.read() #读入
p = re.compile(r'<!--([\s\S]*?)-->') #正则
out = p.findall(content)[1]
res = [c for c in out if c.isalpha()]
print ''.join(res)
正则表达式不能用'<!--(.*)-->'。
1. '.'不能匹配空格,换行符等。
2. '.*'是贪婪匹配,会匹配尽量多的字符。比如content='<body>...</body>','<(.*)>'匹配的结果是'body>...</body'。*号后面加?号,则是以最少的内容匹配。