2017-05-15 67 views
0

我想从声明中提取净利润,并将“净利润”作为非捕获部分。不知道如何做到这一点(可以是非捕获向后看?)在捕获的正则表达式中无法捕获python

'的净利润为创业2350万美元的

所需的O/P:

应用在F ollowing正则表达式:

(net|nt)\s*\.?\s*(profit|earnings)\s*\.?\s*\d+\.?\d*\.?\s*(?:lakh|crore|million) 

但是,它是给

[( '净', '利润')]

作为输出。

+0

是的,你有捕获的组。你看过['re'模块文档](https://docs.python.org/3/library/re.html#regular-expression-syntax)并找到lookbehind断言语法('(?<=。 ..)')呢? IIRC你不能在这样的断言中使用可变宽度模式('*')。也许你只是想让团队*不捕捉*(就像你之后放的团队)? –

+1

您可以使用['ne?t \ s * \。?\ s *(?:利润)\ s *(\。?\ s * \ d + \。?\ d * \??\ s *( ?:lakh | crore | million))'](https://regex101.com/r/7yw1pn/1) – anubhava

+0

Aditya,不[此答案](http://stackoverflow.com/a/43973389/3832970 )为你工作? –

回答

2

尝试用正则表达式下面你将得到的结果在第1组,

(?:ne?t\s(?:profit|earning)\s)([\d\.]+\s(?:million|laks|crore)) 

DEMO

1

可以使用(?:)非捕获

s = 'business venture of net profit 23.5 million dollars' 
re.findall(r'(?:net|nt)\s*\.?\s*(?:profit|earnings)\s*\.?\s*(\d+\.?\d*)\.?\s*(lakh|crore|million)',s) 
[('23.5', 'million')] 
1

你没有捕捉到digitgroup。你也需要用“网”和“利润”

非捕获组所以这应该工作:

编辑捕捉million..etc

import re 
s = 'business venture of net profit 23.5 million dollars' 
re.findall(r'(?:net|nt)\s*\.?\s*(?:profit|earnings)\s*\.?\s*(\d+\.?\d*)\.?\s*(lakh|crore|million)', s) 
# output: ['23.5', 'million'] 

例在: https://regex101.com/r/EXCzeV/2

+0

如何在输出中包含“百万”? –

+0

删除相应的非捕获组:'(lakh | core | million)' – Ludisposed