我试图从以下文本中获得482.75: <span id="yfs_l84_aapl">482.75</span>
我使用的正则表达式是:regex = '<span id="yfs_l84_[^.]*">(.+?)</span>'它工作正常.
但是我不明白的是为什么[^.]*可以在这里匹配aapl?我的理解是.指除换行符之外的任何字符; 和^表示否定者.所以[^.]应该是换行符,[^.]*应该是任意数量的新行.然而,这种理论与现实世界的实施相反.
任何帮助表示赞赏,并提前致谢.
我使用的python代码:
import urllib
import re
htmlfile = urllib.urlopen("http://finance.yahoo.com/q?s=AAPL&ql=0")
htmltext = htmlfile.read()
regex = '<span id="yfs_l84_[^.]*">(.+?)</span>'
pattern = re.compile(regex)
price = re.findall(pattern, htmltext)
print "the price of of aapl is", price[0]
Run Code Online (Sandbox Code Playgroud)
Han*_*ing 27
内[]的.手段只是一个点.领导^意味着"除了......之外的任何东西".
因此[^.]*匹配零个或多个非点.