我想從下面的文本得到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]
Python文檔甚至明確表示:[特殊字符在集合中失去其特殊含義](http://docs.python.org/2/library/re.html#regular-expression-syntax)。 – Evert
這並不回答爲什麼它匹配'aapl' – Anirudha
@Anirudh:是的。 –