0

I am working on extracting certain text from emails using Python Regex

I have tried below:

import re

email = """Hi John,

I am emailing regarding REQ-12345 and REQ-66442.

Many Thanks,

Jane"""


re.findall(r'(?=REQ-)',email)

Expected Output: ['REQ-12345', 'REQ-66442']

Actual Output: ['', '']

I have also tried multiple different things which aren't giving the right results.

How do I achieve the desired output?

2 Answers 2

1

Expected Output: ['REQ-12345', 'REQ-66442']

Do not use zero-length assertion then (it is useful for grabing text after or before something without getting that thing), if REQ- is always followed by 1 or more digits (0123456789) following should suffice

import re
text = "I am emailing regarding REQ-12345 and REQ-66442."
print(re.findall(r'REQ-[0-9]+',text))

output

['REQ-12345', 'REQ-66442']
Sign up to request clarification or add additional context in comments.

Comments

0
import re

email = """Hi John,

I am emailing regarding REQ-12345 and REQ-66442.

Many Thanks,

Jane"""


re.findall(r'(REQ-\d+)',email)
['REQ-12345', 'REQ-66442']

2 Comments

Thanks a ton. Working Exactly. :)
@excelman Great, don't forget to accept the answer if it solves your problem, otherwise, it will keep coming up in the open question list.

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.