-3

I have the following code below:

def convertint(prices):
    output_list = []
    
    for alist in prices:
        print (alist)
        price_int = re.match(r'([0-9]{2,3})', alist)
        print (price_int)
        
#         print (price_int.group())
#         output_list.append(price_int.group())
        
    return output_list
    ###

convertint(['$27' , '$149' , '$59' , '$60' , '$75'])

It's output is below:

$27
None
$149
None
$59
None
$60
None
$75
None

I want my regex match in my code to return 27, 149, 59, etc. without the $ but it's returning None when I try to match it.

What am I doing wrong?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
PineNuts0
  • 4,740
  • 21
  • 67
  • 112

1 Answers1

0

You're missing the $ at the beginning of each item in prices

for alist in prices:
    print(alist)
    price_int = re.match(r'\$([0-9]{2,3})', alist)
    print(price_int.group(1))

$27
27
$149
149
$59
59
$60
60
$75
75
J_Catsong
  • 169
  • 1
  • 9
  • 2
    Note that this may give surprising behavior on input with more than three digits. I would recommend using `fullmatch` instead. – Brian McCutchon Mar 03 '21 at 16:42