gpt4 book ai didi

Python 正则表达式 : Can't extract message containing escaped quotes

转载 作者:行者123 更新时间:2023-12-01 04:15:22 25 4
gpt4 key购买 nike

我正在为 Slack 创建一个机器人,它需要能够从如下所示的数据字符串中提取消息:

[{'text': 'This is my legit "test" \'message\' with "lots" of \'quotation marks\'', 'type': 'message', 'user': '<userID>', 'channel': '<channelID>, 'ts': '1450501889.000074', 'team': '<teamID'}]

我当前的正则表达式是这样的:

re.search(r''''text': (["'])(.*?)\1''', channelstatus)

怎样才能让它只输出以下内容?

This is my legit "test" 'message' with "lots" of 'quotation marks'

最佳答案

不需要正则表达式 - ast.literal_eval() 可以处理:

>>> from ast import literal_eval
>>>
>>> s = r'''[{'text': 'This is my legit "test" \'message\' with "lots" of \'quotation marks\'', 'type': 'message', 'user': '<userID>', 'channel': '<channelID>', 'ts': '1450501889.000074', 'team': '<teamID'}]'''
>>> print(literal_eval(s)[0]["text"])
This is my legit "test" 'message' with "lots" of 'quotation marks'

假设<channelID>后面缺少引号偶然。

关于Python 正则表达式 : Can't extract message containing escaped quotes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34367528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com