【发布时间】:2019-11-11 20:12:46
【问题描述】:
我正在使用来自标题电子邮件的字符串列表中提取代码。看起来像:
text_list = ['Industry / Gemany / PN M564839', 'Industry / France / PN: 575-439', 'Telecom / Gemany / P/N 26-59-29', 'Mobile / France / P/N: 88864839']
到目前为止,我尝试的是:
def get_p_number(text):
rx = re.compile(r'[p/n:]\s+((?:\w+(?:\s+|$)){1})',
re.I)
res = []
m = rx.findall(text)
if len(m) > 0:
m = [p_number.replace(' ', '').upper() for p_number in m]
m = remove_duplicates(m)
res.append(m)
else:
res.append('no P Number found')
return res
我的问题是,我无法提取 ['PN', 'P/N', 'PN:', 'P/N:'] 之前的单词旁边的代码,特别是如果后面的代码以字母开头(即“M”)或者它之间有斜线它(即 26-59-29)。
我想要的输出是:
res = ['M564839','575-439','26-59-29','888489']
【问题讨论】:
标签: python regex python-3.x pandas dataframe