Home > Mobile >  How do I get a regex expression to capture "x dollar(s) and x cents"?
How do I get a regex expression to capture "x dollar(s) and x cents"?

Time:10-14

For example I would like my regex expression to capture both "1 dollar" if there are no cents, or "2 dollars and 71 cents" in my text. I currently have

'\d[\d]*( dollar(s)?)(?:\s*(and )\d[\d]( cent(s)?)\b)?')

and I have tested it out here regexr.com/67etd It seems to work there, but when I run it in python. What regex captures is

(' dollars', 's', '', '', '')

I apologize I am very new to regex, does anyone have any suggestions?

here is my python code:

import re

train = open(r"C:\Users\inigo\PycharmProjects\pythonProject\all-OANC.txt", encoding='utf8')
# didn't have encoding lol
# opens the files
strain = train.read()
# converts the files into a string
train.close()
#pattern = re.compile(r'\$\d[\d,.]*\b(?:\s*million\b)?(?:\s*billion\b)?')
pattern2 = re.compile('\d[\d]*( dollar(s)?)(?:\s*(and )\d[\d]*( cent(s)?)\b)?')

# Finds all numbers which can include commas and decimals that start with $ and if it has a million or a billion at the end
#We need to find patterns so if it contains a dollar keyword afterward it will count the number




matches = pattern2.findall(strain)

for match in matches:

    print(match)

CodePudding user response:

Try this regex:

(\d \s (?:dollars?)(?:\s and\s \d \s cents?)?)\b

Regex Demo

CodePudding user response:

You could use this regex:

'(\d  dollars?)(\s and\s \d{1,2} cents?)?'
  • Related