1

I have a string like below

"‘‘Apple’’ It is create by Steve Jobs (He was fired and get hired) ‘‘Microsoft’’ Bill Gates was the richest man in the world ‘‘Oracle’’ It is a database company"

I am able to manage to use regex to create groups of Apple, Microsoft and Oracle by using ‘‘(.*?)’’ but how should I extract the other sentence part to list?

What I want to create

companyList = ['Apple','Microsoft','Oracle']

descriptionList = ['It is create by Steve Jobs (He was fired and get hired)','Bill Gates was the richest man in the world','It is a database company']

Thank you in advance

1
  • So, if the input is ‘‘Apple’’ or ‘‘AAPL’’ It is create by Steve Jobs (He was fired and get hired) ‘‘Microsoft’’ Bill Gates was the richest man in the world ‘‘Oracle’’ It is a database company, what is the expected output? Also, can there be any or inside ‘‘...’’? Commented Aug 20, 2019 at 10:04

1 Answer 1

3

One option is to use re.findall with the following pattern:

‘‘(.*?)’’ (.*?)(?= ‘‘|$)

This will capture, in separate groups, the company name and description, for each match found in the input. Note that we use the lookahead (?= ‘‘|$) as the end of the current description, which either occurs at the start of the next entry, or the end of the input.

inp = "‘‘Apple’’ It is create by Steve Jobs (He was fired and get hired) ‘‘Microsoft’’ Bill Gates was the richest man in the world ‘‘Oracle’’ It is a database company"
matches = re.findall('‘‘(.*?)’’ (.*?)(?= ‘‘|$)', inp)
companyList = [row[0] for row in matches]
descriptionList = [row[1] for row in matches]
print(companyList)
print(descriptionList)

This prints:

['Apple', 'Microsoft', 'Oracle']
['It is create by Steve Jobs (He was fired and get hired)',
 'Bill Gates was the richest man in the world', 'It is a database company']
Sign up to request clarification or add additional context in comments.

1 Comment

Another way of getting the final lists (no double iteration): companyList, desriptionList = (list(group) for group in zip(*matches))

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.