Home > front end >  How to extract single and double sentence based on starting keyword using regex
How to extract single and double sentence based on starting keyword using regex

Time:10-12

keyword1 = ['Admit:']
Keyword2 = ['Discharge:']

Sentence = "Admit: 15/06/2019 was been in HR hospital. This is usual precaution for accidents. Discharge: 18/06/2019 discharged from A/C room. This is from BRK Hospital.

Tried: (Incorrect code)

import re
a = "Admit: 15/06/2019 was been in HR hospital. This is usual precaution for accidents. Discharge: 18/06/2019 discharged from A/C room. This is from BRK Hospital."
x = re.findall("^Admit: [.]$", a)
print(x)

Required solution:

Admit: 15/06/2019 was been in HR hospital.
Discharge: 18/06/2019 discharged from A/C room. This is from BRK Hospital.

Is there any solution that for Admit: keyword only need to take upto first fullstop and for Discharge: keyword need to take upto two fullstop using regex

CodePudding user response:

Since you are looking for the first full stop after Admit:, you can use this:

x = re.findall("^Admit:[^.]*.", a)

For the second part, you can just allow it to find till the end:

x = re.findall("Discharge:.*", a)

CodePudding user response:

Here is one of the approach:

import re
Sentence = "Admit: 15/06/2019 was been in HR hospital. This is usual precaution for accidents. Discharge: 18/06/2019 discharged from A/C room. This is from BRK Hospital."

admit = re.search(r'(^Admit:.*?)\..*(Discharge:.*)', Sentence)
print (admit.group(1))
# Extract data until second full stop
print ('.'.join(admit.group(2).split('.')[:2]))

Output:

Admit: 15/06/2019 was been in HR hospital
Discharge: 18/06/2019 discharged from A/C room. This is from BRK Hospital
  • Related