Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extract text between strings using regex

I’m trying to extract from the text below the value next to number and the text in between.

Text: The conditions are: number 1, the patient is allergic to dust, number next, the patient has bronchitis, number 4, The patient heart rate is high.

From this text I want to extract the following values:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

(1, the patient is allergic to dust, )

(next, the patient has bronchitis, )

(4, The patient heart rate is high)

I have a pattern that allows me to get the value next to number and the first word of the sentence:

(numbers? (\d+|next)[,.]?\s?(\w+))

This is the result using re.findall

[('number 1, the', '1', 'the'),
 ('number next, the', 'next', 'the'),
 ('number 4, The', '4', 'The')]

As you can using using group I can extract the digit or next value from the text. But I have not been able to extract the entire sentence.

>Solution :

As you . and , and the whitespace chars are optional after the digits or next, you might write the pattern with a non greedy dot asserting numbers again to the right or the end of the string.

\bnumbers? (\d+|next)[,.]?\s?(\w.*?)(?= numbers?\b|\.?$)

Regex demo

import re
 
pattern = r"\bnumbers? (\d+|next)[,.]?\s?(\w.*?)(?= numbers?\b|\.?$)"
 
s = "The conditions are: number 1, the patient is allergic to dust, number next, the patient has bronchitis, number 4, The patient heart rate is high."
 
print(re.findall(pattern, s))

Output

[
('1', 'the patient is allergic to dust,'),
('next', 'the patient has bronchitis,'),
('4', 'The patient heart rate is high')
]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading