1

我正在使用带有 python 的 Zapier 代码操作来尝试从以下消息中提取名称:

_id: 57455fc20913b2f400c2671d
actions: []
authorId: 0d0e129c5913b613a49531b9
name: Tiny Meerkat
received: 1464164290.1
role: appUser
source: {u'type': u'web'}
text: Hello

使用此代码:

import re
name = re.search('(?<=name:)(.*?)(?=received)', input['messages'])
return {
    'name': name if name else 'empty'
}

返回的值始终为空。有谁知道为什么?

4

1 回答 1

0

你可以用这个

name = re.search('(?s)(?<=name:)(.*?)(?=received)', x).group(1).strip("\n\r ")
                  <-->                                 <------>
        Allows . to match new line       Returns content of first captured group

您必须使用(?s),因为字符串分隔在不同的行中。使用(?s),.也匹配新行。

于 2016-05-25T09:58:23.793 回答