📜  python - 从字符串中提取价格 - Python 代码示例

📅  最后修改于: 2022-03-11 14:45:30.796000             🧑  作者: Mango

代码示例1
# Visit: https://regexr.com/
# and look at the Menu/Cheatsheet

# Extract the price from a string
price = 'Price is: $520,130.250'
expr = 'Price is: \$([0-9,]*\.[0-9]*)'

match = re.search(expr, price)
print(match.group(0))              # give entire match
print(match.group(1))              # give only text in brackets

price_without_comma = match.group(1).replace(',', '')     # replace comma because can't be converted to a number
price_num = float(price_without_comma)                    # convert string to float 
print(price_num)