【问题标题】:How to properly use Regular Expressions in python? [closed]如何在python中正确使用正则表达式? [关闭]
【发布时间】:2021-12-14 18:48:36
【问题描述】:

我想知道如何分隔这个字符串:

"Venda R$ 3.780.000,00  Aluguel  Condomínio R$ 1.980,00 IPTU R$ 7.990,00 Resumo de Custo"

所以它可以是:3780000、1980 和 7990。我是正则表达式的新手,所以详细的解释会很有帮助。

【问题讨论】:

    标签: python python-3.x python-re


    【解决方案1】:

    你需要一个正则表达式来匹配

    • [\d.] 数字或点

    • [\d.]{3,} 超过 3 个字符

    然后去掉点并转换为int

    import re
    
    values = "Venda R$ 3.780.000,00  Aluguel  Condomínio R$ 1.980,00 IPTU R$ 7.990,00 Resumo de Custo"
    result = [int(x.replace(".", "")) for x in re.findall(r"[\d.]{3,}", values)]
    print(result)  # [3780000, 1980, 7990]
    

    【讨论】:

      【解决方案2】:

      我给你一个总体框架,然后指出一些问题。

      您首先要用文字描述您想要什么。您需要一个数字,后跟一组数字或逗号或句点,以数字结尾。那是:

      import re
      srch = "Venda R$ 3.780.000,00  Aluguel Condominio R$ 1.980,00 IPTU R$ 7.990,00 Resumo de Custo"
      
      f = re.findall( r"\d[0-9.,]*\d", srch )
      print(f)
      

      这给出了:

      ['3.780.000,00', '1.980,00', '7.990,00']
      

      如果需要,您可以将其转换为数字,去掉句点并去掉美分。

      【讨论】:

        猜你喜欢
        • 2017-11-13
        • 2021-09-17
        • 2015-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-27
        • 2011-03-04
        相关资源
        最近更新 更多