我正在尝试从一系列字符串中提取薪水。 我正在使用正则表达式的findall()函数,但它也返回许多空字符串以及薪水,这在我的代码后面会导致问题。
sal= '41 000€ à 63 000€ / an' #this is a sample string for which i have errors
regex = ' ?([0-9]* ?[0-9]?[0-9]?[0-9]?)'#this is my regex
re.findall(regex,sal)[0]
#returns '41 000' as expected but:
re.findall(regex,sal)[1]
#returns: ''
#Desired result : '63 000'
#the whole list of matches is like this:
['41 000',
'',
'',
'',
'',
'',
'',
'63 000',
'',
'',
'',
'',
'',
'',
'',
'',
'']
# I would prefer ['41 000','63 000']
有人能帮忙吗? 谢谢
r'(?<!\d)\d{1,3}(?: \d{3})*(?!\d)'
。 - Wiktor Stribiżew(\d+(?: \d{1,3})?)€
并使用 findall 方法来仅返回薪水。演示 - The fourth bird€
的数字吗?尝试使用r'(?<!\d)(\d{1,3}(?:[ \xA0]\d{3})*)\s*€'
或r'(?<!\d)(\d+|\d{1,3}(?:[ \xA0]\d{3})*)\s*€'
。请参见 https://regex101.com/r/rwbpTx/1 - Wiktor Stribiżew