regex 股票行情的正则表达式- Python

u59ebvdq  于 2022-12-30  发布在  Python
关注(0)|答案(3)|浏览(128)

我有一个推文列表。它们看起来像这样:

data = [['trading $aa $BB stock market info'],
        ['$aa is $116 market is doing well $cc $ABC']]

我想提取股票行情:

['$aa', '$BB']
['$aa', '$cc', '$ABC']]

我已经试过了:

for i in data:
    print re.findall(r'[$]\S*', str(i))

并且,输出也包含$116:

['$aa', '$BB']
['$aa', '$116', '$cc', '$ABC']]

有什么建议吗?

q0qdq0h2

q0qdq0h21#

匹配美元符号、一个字母,然后是空格以外的任何字符:

re.findall(r'[$][A-Za-z][\S]*', str(i))
ccrfmcuu

ccrfmcuu2#

reticker通过根据其配置创建一个自定义正则表达式来实现这一点。它使用创建的模式从文本中提取代码。或者,返回的模式可以单独使用。

>>> import reticker

>>> extractor = reticker.TickerExtractor()
>>> type(extractor.pattern)
<class 're.Pattern'>

>>> reticker.TickerExtractor().extract("Comparing FNGU vs $WEBL vs SOXL- who wins? And what about $cldl vs $Skyu? BTW, will the $w+Z pair still grow? IMHO, SOXL is king! [V]isa is A-okay!")
["FNGU", "WEBL", "SOXL", "CLDL", "SKYU", "W", "Z", "V", "A"]

>>> reticker.TickerExtractor().extract("Which of BTC-USD, $ETH-USD and $ada-usd is best?\nWhat about $Brk.a and $Brk.B? Compare futures MGC=F and SIL=F.")
['BTC-USD', 'ETH-USD', 'ADA-USD', 'BRK.A', 'BRK.B', 'MGC=F', 'SIL=F']
fcy6dtqo

fcy6dtqo3#

我将把它放在这里,以便人们查找与股票代码匹配的正则表达式

re.fullmatch('([A-Za-z]{1,5})(-[A-Za-z]{1,2})?', symbol)

相关问题