[英]find all words in a text string beginning with $
我正在尝试运行 python 脚本来解析一些文本并保存所有以“$”开头的单词。
我正在运行的代码行是:
for post in posts
ticker = re.findall(r'\b[$]\w+', post.title)
tickers.append([ticker])
当我运行这个脚本时,output 是:
[[[]], [[]], [[]], [[]], [[]], [[]], [[]], [[]], [[]]]
我在想我的正则表达式只有一个错误,但我似乎无法在任何地方找到解决方案。
看看re
。 “$”是一个特殊字符,标记字符串的结尾。 你必须逃避它。
可能您正在寻找的内容:
ticker = [re.findall(r'\$\w+', post.title) for post in posts]
如果您想排除空列表/日志
# bool([]) == False
# bool([...]) == True
ticker = list(filter(bool, re.findall(r'\$\w+', post.title) for post in posts))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.