# Regular expression in python -

I want to extract the values of the below text

Paﬁent Name : Thomas Joseph MRNO : DQ026151?
Doctor : Haneef M An : 513! Gandar : Male
Admission Data : 19-Feb-2V'3‘¥T12:2'$PM Bill No : IDOGIII.-H-17 Discharge Date : 22-Feb-20$? 1D:5‘F AM Bill Dale : E2-Feb-2017


extract only the values of the field names for example,

Thomas Joseph from the field name Pateint name, similarly for others field names and save the output to excel

Python code for the above

My attempt -

text = pt.image_to_string(img1)
print(text)
s = re.findall(r'\s:\s(\w+)', text)
print (s)

• This job would be quite easier if you know what fields you will have in the text. – Kiritee Gak Apr 10 '17 at 6:57
• These are the field names. Paﬁent Name , MRNO , Doctor , An, Gandar , Admission Data, Bill No ,Discharge Date,Bill Dale – Shyama Apr 10 '17 at 7:00
• I want to extract the values from these field names..like Patient name is the field name then i need to extract "Thomas Joseph" – Shyama Apr 10 '17 at 7:01
• what I am saying is, are these field names always the same or they change from doc to doc or will there be addn of new fields sometimes? – Kiritee Gak Apr 10 '17 at 7:04
• I'm voting to close this question as off-topic because this is just a python regex question and you'd get a quicker answer on StackOverflow. – Spacedman Apr 10 '17 at 7:33

## 2 Answers

It may not be perfect but does the job almost.

import re
re.findall(r'(?<=: )\w{2}-\w{3}-\d{4}|(?<=: )\d{2}-\w{3}-\w{2}|(?<=: )\s?\w+\s?\w+\s?\w+',data)

#['Thomas Joseph MRNO','DQ026151','Haneef M An','513','Male','19-Feb-2V','IDOGIII','22-Feb-20','E2-Feb-2017']

• Ya this seems better. but still cudnt extract all values of the fieldnames – Shyama Apr 10 '17 at 9:48
• ['Thomas Joseph MRNO', 'DQ026151', 'Haneef M An', '513', 'Male\nAdmission Data', '19-Feb-2V', 'IDOGIII', '22-Feb-20', 'E2-Feb-2017'] – Shyama Apr 10 '17 at 9:51
• above comment was my output. i need still more better output – Shyama Apr 10 '17 at 9:52
• Can you please explain me the above code. – Shyama Apr 10 '17 at 11:07
• Check the chat room. – Jil Jung Juk Apr 10 '17 at 11:51

As @spacedman correctly mentioned, this will answered quicker at StackOverflow. But you can use this to create a dictionary like this. There might be a better way but this is a quick work around.

# -*- coding: utf-8 -*-
import re
st = '''Pafient Name : Thomas Joseph MRNO : DQ026151?
Doctor : Haneef M An : 513! Gandar : Male
Admission Data : 19-Feb-2V'3‘¥T12:2'$PM Bill No : IDOGIII.-H-17 Discharge Date : 22-Feb-20$? 1D:5‘F AM Bill Dale : E2-Feb-2017'''
st = st.decode('utf-8').replace('\n','')+'<eof>'
words = ['Pafient Name','MRNO','Doctor','An','Gandar','Admission Data','PM Bill No','Discharge Date','Bill Dale','<eof>']
print {words[i]:st[st.index(words[i])+len(words[i]):st.index(words[i+1])].replace(':','').strip() for i in range(len(words)-1)}

• I tried to execute this.i got this error – Shyama Apr 10 '17 at 7:48
• Well, invest some of your time onto it and post that error onto StackOverflow if you are unable to resolve it. – Kiritee Gak Apr 10 '17 at 7:58
• File "<ipython-input-6-72de8d228cb9>", line 8 print (words[i]:st[st.index(words[i])+len(words[i]):st.index(words[i+1])].replace(':','').strip() ^ SyntaxError: invalid syntax – Shyama Apr 10 '17 at 8:56
• Try it in python 2.7, you might be using 3.x. Either way remove the dictionary comprehension and write a normal loop and try to print the substrings and then debug and find the type of error. This should give u some clue. – Kiritee Gak Apr 10 '17 at 9:24
• In which version of Python did u execute the above code.can u show me the output u have got. – Shyama Apr 10 '17 at 9:26