Search code examples
regexpython-3.xdata-extraction

fetching value from same line and next line


I have a text file which has data in this format. I want the fetch the doctor's name. May I know what would be the regular expression or any other approach which would handle both the cases

The patient is referred by Dr. Zach Foster.


The patient is referred by
Dr. Corey Piccirillo



Output:
Dr. Zach Foster
Dr. Corey Piccirillo

I am using the below regular expression for first case then using Spacy to fetch out the name:

re(r'.*referred by.*',re.I)

For the second case:

for line in file:  
   if "referred by" in line:
       print(next(ifile, '').strip())

Solution

  • re(r'by[\n\s](Dr.+\.*)',re.I)
    

    Would match both cases in capture groups.