在“ for”一词之间提取单词和开头的括号“(“在电子邮件主题行中”.电子邮件主题行是输入



客户端的名称在" for"one_answers"开放括号之前"之后"("启动提案编号。我需要提取客户名称才能使用以查找交易未来的步骤。设置此设置的最简单方法是什么?使用Zapier提取物模式或在Python中使用Zapier代码?

我已经尝试过,但它没有起作用。

似乎很有希望。

input_data

客户端=提醒:Leruths已向您发送了一个有关业务名称(#642931(的建议

import regex
rgx = regex.compile(r'(?si)(?|{0}(.*?){1}|{1}(.*?)
{0})'.format('for', '('))
s1 = 'client'
for s in [s1]:
m = rgx.findall
for x in m:
print x.strip()

我也尝试过,它不起作用。

start = mystring.find( 'for' )
end = mystring.find( '(' )
if start != -1 and end != -1:
result = mystring[start+1:end]

我正在寻找在我的示例中返回的业务名称。

最快的方法:

start = client.find('for')
end = client.find('(')
result = client[start+4:end-1]
print(result)

带有正则:

result = re.search(r' for (.*) [(]', client)
print(result.group(1))

可能有一种更干净的方法可以做到这一点,但这是没有正则

的另一个解决方案
client = "Reminder: Leruths has sent you a proposal for Business Name (#642931)"
cs = client.split(" ")
name = ""
append = False
for word in cs:
    if "for" == word:
        append = True
    elif word.startswith("("):
        append = False
    if append is True and word != "for":
        name += (word + " ")
name = name.strip()
print(name)

另一种方法:

client = "Reminder: Leruths has sent you a proposal for Business Name (#642931)"
cs = client.split(" ")
name = ""
forindex = cs.index("for")
for i in range(forindex+1, len(cs)):
    if cs[i].startswith("("):
        break
    name += cs[i] + " "
name = name.strip()
print(name)

运行下面的代码给出:

Regex method took 2.3912417888641357 seconds
Search word by word method took 4.78193998336792 seconds
Search with list index method took 3.1756017208099365 seconds
String indexing method took 0.8496286869049072 seconds

代码以检查最快的代码以获得超过一百万次尝试的名称:

import re
import time
client = "Reminder: Leruths has sent you a proposal for Business Name (#642931)"
def withRegex(client):
    result = re.search(r' for (.*) [(]', client)
    return(result.group(1))
def searchWordbyWord(client):
    cs = client.split(" ")
    name = ""
    append = False
    for word in cs:
        if "for" == word:
            append = True
        elif word.startswith("("):
            append = False
        if append is True and word != "for":
            name += (word + " ")
    name = name.strip()
    return name
def searchWithListIndex(client):
    cs = client.split(" ")
    name = ""
    forindex = cs.index("for")
    for i in range(forindex+1, len(cs)):
        if cs[i].startswith("("):
            break
        name += cs[i] + " "
    name = name.strip()
    return name
def stringIndexing(client):
    start = client.find('for')
    end = client.find('(')
    result = client[start+4:end-1]
    return result
wr = time.time()
for x in range(1,1000000):
    withRegex(client)
wr = time.time() - wr
print("Regex method took " + str(wr) + " seconds")
sw = time.time()
for x in range(1,1000000):
    searchWordbyWord(client)
sw = time.time() - sw
print("Search word by word method took " + str(sw) + " seconds")
wl = time.time()
for x in range(1,1000000):
    searchWithListIndex(client)
wl = time.time() - wl
print("Search with list index method took " + str(wl) + " seconds")
si = time.time()
for x in range(1,1000000):
    stringIndexing(client)
si = time.time() - si
print("String indexing method took " + str(si) + " seconds")

相关内容

最新更新