-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
  • 561,645
  • 34
  • 376
  • 476
PineNuts0
  • 4,050
  • 16
  • 57
  • 93

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
  • 99
  • 8
  • 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