正则表达式中[^。] *的含义是什么?

时间:2013-09-30 08:36:34

标签: python regex

我正试图从以下文字中获得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]

2 个答案:

答案 0 :(得分:23)

[] .内只是一个点。领先的^意味着“除......之外的任何东西”。

所以[^.]*匹配零个或多个非点。

答案 1 :(得分:3)

。字符匹配中的点只意味着点,字面意思。

不同的语法和特殊字符( - 用于范围的破折号,用于否定的^)适用于字符匹配规范。其他模式语法不适用。