Home > Software engineering >  Extract text between two dots containing a specific word
Extract text between two dots containing a specific word

Time:10-03

I'm trying to extract a sentence between two dots. All sentences have inflam or Inflam in them which is my specific word but I don't know how to make that happen.

what I want is ".The bulk of the underlying fibrous connective tissue consists of diffuse aggregates of chronic inflammatory cells." or ".The fibrous connective tissue reveals scattered vascular structures and possible chronic inflammation." from a long paragraph

CodePudding user response:

import re

string=''  # replace with your paragraph 

print(re.search(r"\.[\s\w]*\.",string).group()) #will print first matched string
print(re.findall(r"\.[\s\w]*\.",string)) #will print all matched strings

CodePudding user response:

You can try by checking for the word in every sentence of the text.

for sentence in text.split("."):
    if word in sentence:
        print(sentence[1:])

Here you do exactly that and if you find the word, you print the sentence without the space in the start of it. You can modify it in any way you want.

  • Related