Python regex findall在没有询问时返回空字符串

问题描述 投票:1回答:1

我正在尝试从字符串列表中提取工资。我正在使用正则表达式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']

有人可以帮忙吗?谢谢

python regex string list findall
1个回答
3
投票

使用re.findall会在你的模式中使用它们时为你提供捕获组,并且你正在使用一个几乎所有东西都是可选的组,在结果中给你空字符串。

在你的模式中你使用[0-9]*,它将匹配数字的0+次。如果对前导数字没有限制,您可以使用[0-9]+而不是使其成为可选项。

您可以将此模式与捕获组一起使用:

(?<!\S)([0-9]+(?: [0-9]{1,3})?)€(?!\S)

Regex demo | Python demo

说明

  • (?<!\S)断言左边的内容不是非空格字符
  • (捕获组 [0-9]+(?: [0-9]{1,3})?匹配1+位数后跟一个匹配空格和1-3位数的可选部分
  • )关闭捕获组
  • 字面意思匹配
  • (?!\S)断言右边的内容不是非空白字符

您的代码可能如下所示:

import re
sal= '41 000€ à 63 000€ / an' #this is a sample string for which i have errors
regex = '(?<!\S)([0-9]+(?: [0-9]{1,3})?)€(?!\S)'
print(re.findall(regex,sal))  # ['41 000', '63 000']
© www.soinside.com 2019 - 2024. All rights reserved.