python - 使用正则表达式检索特定文本

标签 python regex python-3.x twitter

tweets = re.findall(r "'text':+.*'truncated'", tweets)

print (tweets)

'text': "RT @premierleague: 🔵 @WayneRooney's chase is on 👀", 'truncated':

我有一个像上面这样的文本字符串,我想检索 'text': 和 'truncated' 之间的推文

我已经编写了上面的代码,但收到错误消息

 tweets = re.findall(r "'text':+.*'truncated'", tweets)
                                                ^
SyntaxError: invalid syntax

我正在使用 findall,因为推文会重复,并且我想从 findall 搜索中检索所有推文。

谢谢。

最佳答案

无效语法错误是由于r和正则表达式之间的空格造成的:

tweets = re.findall(r"'text':+.*'truncated'", tweets)
print(tweets)

返回:

['\'text\': "RT @premierleague: \xf0\x9f\x94\xb5 @WayneRooney\'s chase is on \xf0\x9f\x91\x80", \'truncated\'']

仅检索文本:

tweets = re.findall(r"'text':+(.*)'truncated'", tweets)
print(tweets)

返回:

 "RT @premierleague: 🔵 @WayneRooney's chase is on 👀", 

关于python - 使用正则表达式检索特定文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45648130/

相关文章:

asp.net - 匹配 "True"或 "False"的正则表达式

python - pip(python2)和pip3(python3)可以共存吗?

python - 二维数组每一列的外积形成一个三维数组 - NumPy

python - 为什么这本字典会变成一个元组?

c# - 具有最大长度的 float 的正则表达式

正则表达式匹配反斜杠星

python - 导入错误 : No module named 'requests'

c++ - 结合python和c++,或者cython,优化一个函数;最大似然示例;对c++的了解很少

python - 使用 Wikipedia 上的 BeautifulSoup 进行网页抓取

python - 使用队列 Tensorflow 训练模型