reading $, . and number in python regex -
i want able read price in given line, example:
$.0001 ...
i'm trying read $.0001
part following regex:
new_string = re.search(r'\$([.]\d+)', description)
what different approaches same problem?
for single data point you've suggested, current pattern work fine. however, if may need match against more diverse range of values, might need more flexible pattern.
for instance, if it's possible values of $1 or more appear in input, you'll need able match digits before decimal point:
new_string = re.search(r'\$(\d*[.]\d+)', description) # can match "$1.001"
further, if might whole number values without decimals, might need make decimal point , following digits optional:
new_string = re.search(r'\$(\d*(?:[.]\d+)?)', description) # can match "$2"
finally, note on style. i'm not sure if there regex style guides out there, personal taste use escape sequence decimal point, rather one-character character class. is, i'd use \.
instead of [.]
. doesn't work differently though, , taste may differ mine, use (and collaborating programmers) feel comfortable with.
Comments
Post a Comment