1

I am trying to split R15.49 to (R, 15.49) or

ZAR15.49 to (ZAR, 15.49)

I have tried one of the solutions here and implememted the function below:

def splitCurrency(string):    
    match = re.search(r'([\D]+)([\d,]+)', string)
    output = (match.group(1), match.group(2).replace(',',''))
    return output

But I am getting (R, 15) or (ZAR, 15). and its ignoring the digits after the decimal place

tendaitakas
  • 328
  • 5
  • 18

1 Answers1

2

If you want to fish out these values from a larger text, then use re.findall:

inp = "R15.49 or ZAR15.49"
matches = re.findall(r'\b([A-Z]+)(\d+(?:\.\d+)?)\b', inp)
print(matches)

This prints:

[('R', '15.49'), ('ZAR', '15.49')]
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Thank you. Actually your first answer before your edit is what I am looking for. I guess my wording was a bit confusing and I have edited the question – tendaitakas Nov 26 '20 at 06:29
  • By it is still resulting in an error 'unterminated subpattern at position 0'. If you can please review your first answer. Thanks – tendaitakas Nov 26 '20 at 06:30
  • Reload the page. My initial suggestion to use `re.split` is not viable. The code you see above has been tested and is working. – Tim Biegeleisen Nov 26 '20 at 06:31