如何通过Python Regex查找带有美元符号,千位分隔符和小数点的价格标签

时间:2018-12-10 18:48:36

标签: python regex

我想从以下字符串中解析出“ $ 1000.00 ”(一个字符串):

等待了近三个月后,丢了一个书架($ 1,000.00)。

我应该如何在re.findall()函数中设计正则表达式模式?谢谢!

我尝试过的是re.findall(r"\d+[.,]?\d+", text),但我只有: ['1,000','00']

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

1 个答案:

答案 0 :(得分:1)

  

如何查找带有美元符号,千位分隔符和十进制的价格标签   Python正则表达式指向


money_parser egg正是您需要的:

安装:

        switch ((sender as Button).Text)
        {

            case "1":
                serialMonitor.PrintLine("1");
                currentPressedCode = currentPressedCode + "1";
                break;
            case "2":
                serialMonitor.PrintLine("2");
                currentPressedCode = currentPressedCode + "2";
                break;
            case "3":
                serialMonitor.PrintLine("3");
                currentPressedCode = currentPressedCode + "3";
                break;
            default:
                break;
        }  if (buttonsPressed == 3)
        {
            if (currentPressedCode == vaultCode)
            {

                //vault open
                serialMonitor.PrintLine("vault");
                pcbGreen.BackColor = Color.ForestGreen;

            }
        }
        else
        {
            // wrong code
            serialMonitor.PrintLine("wrong");
            MessageBox.Show("Wrong password"); // wrong password messagebox
            pcbRed.BackColor = Color.DarkRed;
        }

用法:

pip install money_parser

旧答案

您可以使用类似的内容:

from money_parser import price_str
x = "lost a bookcase ($1,000.00) after waiting for nearly three months."
print(price_str(x))
# 1000.00

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


Regex Demo
Python Demo


正则表达式说明 enter image description here


注意:

  1. 不是防弹正则表达式['1,000.00'],但足以让您 开始。
  2. money_parser使用的
  3. 正则表达式

\b\$?[\d,.]+\b