【发布时间】:2018-05-18 15:12:05
【问题描述】:
我有一个关键字列表,我想解析关键字的长字符串列表、货币格式的价格以及字符串中小于 10 的任何其他数字。例如:
keywords = ['Turin', 'Milan' , 'Nevada']
strings = ['This is a sentence about Turin with 5 and $10.00 in it.', ' 2.5 Milan is a city with £1,000 in it.', 'Nevada and $1,100,000. and 10.09']]
希望返回以下内容:
final_list = [('Turin', '$10.00', '5'), ('Milan', '£1,000', '2.5'), ('Nevada', '$1,100,000', '')]
我有以下功能正则表达式,但我不知道如何将输出组合到元组列表中。有没有更简单的方法来实现这一点?我应该按单词拆分然后查找匹配项吗?
def find_keyword_comments(list_of_strings,keywords_a):
list_of_tuples = []
for string in list_of_strings:
keywords = '|'.join(keywords_a)
keyword_rx = re.findall(r"^\b({})\b$".format(keywords), string, re.I)
price_rx = re.findall(r'^[\$\£\€]\s?\d{1,3}(?:[.,]\d{3})*(?:[.,]\d{1,2})?$', string)
number_rx1 = re.findall(r'\b\d[.]\d{1,2}\b', string)
number_rx2 = re.findall(r'\s\d\s', string)
【问题讨论】:
-
你会如何处理像“一张从都灵到米兰的机票花了我 100 美元”这样的句子?
-
理想情况下,重复项将捕获最接近价格和数量的值。
标签: python regex python-3.x nlp