如何用Python正则表达式查找带美元符号、千位分隔符和小数点的价签

2024-05-15 05:50:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从下面的字符串中一段(字符串)中解析出“$1000.00”:

在等待了近三个月后丢失了一个书架($1000.00)。

我的正则表达式应该如何设计关于芬德尔()功能?谢谢!在

我尝试的是re.findall(r"\d+[.,]?\d+", text),我只得到: ['1000','00']

但是,我希望我的输出是: ['$1000.00']


Tags: 字符串text功能re书架findall
1条回答
网友
1楼 · 发布于 2024-05-15 05:50:57

How to find Price Tag with Dollar Sign, thousand delimiter AND decimal point by Python Regex


money_parser egg正是您需要的:

安装:

pip install money_parser

用法:

^{pr2}$

旧答案

您可以使用类似于:

x = "lost a bookcase ($1,000.00) after waiting for nearly three months."
result =  re.findall(r"\b\$?[\d,.]+\b", x)

['1,000.00']


Regex Demo
Python Demo


正则表达式解释enter image description here


注意事项:

  1. 不是一个防弹的regex\b\$?[\d,.]+\b,但足以让您 起动。在
  2. money_解析器使用的regex

^{4}$

相关问题 更多 >

    热门问题