0

I want to extract net profit from the statement, with 'net profit' as the non capturing part. Not sure how to do it(may be a non capturing look behind?)

eg

'business venture of net profit 23.5 million dollars'

required o/p:

23.5 million

Applied the following regex:

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

But, it is giving

[('net', 'profit')]

as the output.

4
  • Yes, you have groups that capture. Did you look at the re module documentation and find the lookbehind assertion syntax ((?<=...)) yet? IIRC you can't use variable-width patterns (* specifically) in an assertion like that however. Perhaps you just wanted t omake the groups non-capturing (like the group you put after)? Commented May 15, 2017 at 7:02
  • 1
    You can use ne?t\s*\.?\s*(?:profit|earnings)\s*(\.?\s*\d+\.?\d*\.?\s*(?:lakh|crore|million)) Commented May 15, 2017 at 7:03
  • Aditya, doesn't this answer work for you? Commented May 15, 2017 at 7:45
  • Yes, it does @WiktorStribiżew Commented May 15, 2017 at 9:06

3 Answers 3

2

Try with below regex you will get the result in group 1,

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

DEMO

Sign up to request clarification or add additional context in comments.

Comments

1

You didn't capture the digitgroup. Also you need a non-capturing group with the 'net' and 'profit'

so this should work:

Edit to capture 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']

Example at: https://regex101.com/r/EXCzeV/2

2 Comments

how to include 'million' in the output?
Remove the non-capturing group which corresponds to : (lakh|core|million)
1

You can use (?:) for non-capture

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')]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.