尝试在 Zapier 中使用 python 导入字符串

Trying to import a string using python in Zapier

我将 Zapier 代码操作与 python 结合使用,尝试从以下消息中提取姓名:

_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'
}

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

你可以用这个

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). 也匹配新行。