python - 在电子邮件主题行中提取单词“for”和左括号“(”之间的单词。电子邮件主题行是输入
问题描述
客户的名字在“for”这个词之后和开始提案编号的左括号“(”之前。我需要提取客户名称以在以后的步骤中查找交易。什么是最简单的方法设置这个?使用 Zapier 提取模式还是在 Python 中使用 Zapier 代码?
我已经尝试过了,但它没有用。不过,这似乎很有希望。
输入数据
客户 = 提醒: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")
推荐阅读
- sql - 在 SQL Server 中插入递归 CTE
- php - 如何对 Codeigniter 中的 Composer 自动加载进行故障排除?
- html - 为 DIV 正方形制作对角线
- arduino - Arduino 计时器计数重置为 65,但应重置为 70
- java - 如何通过java查找列是否为“IDENTITY”列
- java - Cassandra Talend Job 运行通过 java 构成错误
- python - 在 python 中打印元组的不一致
- selenium - 如何在 Selenium 版本 3.4.0 中使用显式等待
- python - 在使用 OCR 从图像中提取文本期间,python 中的子进程库出现问题
- javascript - 从 chrome.storage.sync 中删除数据?