繁体   English   中英

Python Regex获取关键字后括号内外的数字

[英]Python Regex to get numbers inside and outside brackets after key word

我需要一个正则表达式函数来获取经营利润/(损失)关键字后的数字,即 1,644 193 1,837 1,280 (263) 1,017(见下文); 我尝试了许多正则表达式函数,但它们不起作用。

文本看起来是这样的:

持续经营 收入 2 57,491 – 57,491 55,917 – 55,917 销售成本 (54,092) (49) (54,141) (52,899) (116) (53,015) 毛利/(亏损) 39),816,203,203,203,039物业相关项目产生/(亏损) (1,786) 31 153 89 (1,633) 120 (1,734) (4) (261) 114 (1,995) 110 营业利润/(亏损) 1,644 193 1,837 (26017) 1,281合营企业和联营企业的税后利润/(亏损) 财务收入 财务成本 13 5 5 (6) 98 (593) – – (38) (6) 98 (631) (30) 109 (630) (77) – (244) (107) 109 (874) 税前利润/(亏损) 1,143 155 1,298 729 (584) 145 税收 6 (286) (20) (306) (185) 98 (87) 利润/(亏损)年度持续经营业务 857 135 992 544 (486) 58 已终止经营业务 已终止经营业务年度利润/(亏损) 7 – 216 216 (37) (75) (112) 年度利润/(亏损) 857 351 1,208 507 (561) (54)

你可以在这里尝试re.findall

inp = "Continuing operations Revenue 2 57,491 – 57,491 55,917 – 55,917 Cost of sales (54,092) (49) (54,141) (52,899) (116) (53,015) Gross profit/(loss) 3,399 (49) 3,350 3,018 (116) 2,902 Administrative expenses Profits/(losses) arising on property-related items (1,786) 31 153 89 (1,633) 120 (1,734) (4) (261) 114 (1,995) 110 Operating profit/(loss) 1,644 193 1,837 1,280 (263) 1,017 Share of post-tax profits/(losses) of joint ventures and associates Finance income Finance costs 13 5 5 (6) 98 (593) – – (38) (6) 98 (631) (30) 109 (630) (77) – (244) (107) 109 (874) Profit/(loss) before tax 1,143 155 1,298 729 (584) 145 Taxation 6 (286) (20) (306) (185) 98 (87) Profit/(loss) for the year from continuing operations 857 135 992 544 (486) 58 Discontinued operations Profit/(loss) for the year from discontinued operations 7 – 216 216 (37) (75) (112) Profit/(loss) for the year 857 351 1,208 507 (561) (54)"
matches = re.findall(r'Operating profit/\(loss\)\s+([0-9,() ]+)\s+', inp)
print(matches[0])

这打印:

1,644 193 1,837 1,280 (263) 1,017

尝试以下正则表达式:

"Operating profit\/\(loss\) ([0-9, ()]*)"

我建议使用regex101为您找到合适的正则表达式:

在此处输入图片说明

你可以这样做:(使用组)

import re

string="Continuing operations Revenue 2 57,491 – 57,491 55,917 – 55,917 Cost of sales (54,092) (49) (54,141) (52,899) (116) (53,015) Gross profit/(loss) 3,399 (49) 3,350 3,018 (116) 2,902 Administrative expenses Profits/(losses) arising on property-related items (1,786) 31 153 89 (1,633) 120 (1,734) (4) (261) 114 (1,995) 110 Operating profit/(loss) 1,644 193 1,837 1,280 (263) 1,017 Share of post-tax profits/(losses) of joint ventures and associates Finance income Finance costs 13 5 5 (6) 98 (593) – – (38) (6) 98 (631) (30) 109 (630) (77) – (244) (107) 109 (874) Profit/(loss) before tax 1,143 155 1,298 729 (584) 145 Taxation 6 (286) (20) (306) (185) 98 (87) Profit/(loss) for the year from continuing operations 857 135 992 544 (486) 58 Discontinued operations Profit/(loss) for the year from discontinued operations 7 – 216 216 (37) (75) (112) Profit/(loss) for the year 857 351 1,208 507 (561) (54)"

s=re.search('Operating profit\/\(loss\) ([0-9, ()]*)',string)    
answer=s.group(1)
print answer

这将打印输出如下:

1,644 193 1,837 1,280 (263) 1,017 

请检查LINK 中的代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM