gpt4 book ai didi

python - 正则表达式python同时找到美元金额和几个字

转载 作者:太空宇宙 更新时间:2023-11-04 02:45:15 25 4
gpt4 key购买 nike

我需要在一个段落中同时找到美元金额和围绕该金额的几个(3 或 4)个词。

in-process research and development of $184.3 million and charges $120 of 
million for the impairment of long-lived assets. See Notes 2, 16 and21 to the
Consolidated Financial Statements. Income from continuingoperations for the
fiscal year ended September 30, 2001 also includes a netgain on sale of
businesses and investments of $276.6 million and a net gainon the sale of
common shares of a subsidiary of $64.1 million.

我想得到的是下面这样的东西, [amount, amount+数字字,amount前3-4字]。

[$184.3 $184.3 million, research and development of $184.3 million],[$120, $120 of million,charges $120 of 
million for the impairment of long-lived assets ], [$276.6, $276.6 million, investments of $276.6 million] ,[ $64.1, $64.1 million, a subsidiary of $64.1 million.]

我试过的是这个,它只找到了美元金额。

[\$]{1}\d+\.?\d{0,2}

谢谢!

最佳答案

那么让我们为您的模式命名:

amount_patt = r"[\$]{1}[\d,]+\.?\d{0,2}"

然后应使用上述定义数字词:

digit_word_patt = amount_patt + r" (\w+)"

现在,对于周围的 3-4 个词,执行以下操作:

words_patt = r"(\S+ ){3, 4}" + amount_patt + r"(\S+ ){3, 4}"

大功告成!现在只需将它们与您的 re 方法一起使用即可提取字符串。

关于python - 正则表达式python同时找到美元金额和几个字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45312315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com