-1

text = ' My uncle is admitted in the hospital.\n He has been suffering from 10 months.\n The address of the hospital is \nApollo Health City Campus, Jubilee Hills, Hyderabad - 500 033. '

now i want to extract the address of the hospital i.e ' Apollo Health City Campus, Jubilee Hills, Hyderabad -** ' using regex lookbehind, i used the following code but i want a expression that can help me extract the strings uptill the \n character prior to the six digit pincode i.e '500 033' Currently am trying to extract 6 strings but i want a regex expression that can help me get all the strings till '\n' .

r'((\w\S+\s+){1,6})(?=500 033)'

expected output - ' Apollo Health City Campus, Jubilee Hills, Hyderabad - ' i.e all strings before \n

1 Answer 1

2

why not simply split using \n and get the last string?

text = ' My uncle is admitted in the hospital.\n He has been suffering from 10 months.\n The address of the hospital is \nApollo Health City Campus, Jubilee Hills, Hyderabad - 500 033. '
print(text.split('\n')[-1])

If you are sure that the string will contain a pincode. i.e 6 digit number, another approach could be:

text = ' My uncle is admitted in the hospital.\n He has been suffering from 10 months.\n The address of the hospital is \nApollo Health City Campus, Jubilee Hills, Hyderabad - 500 033. '
for x in text.split('\n'):
    if [idx.isdigit() for idx in x].count(True)==6:
        print(x)

I have added a check for 6 digit in a string only. you can modify according to your needs.

4
  • there can be more text after the line too, so that's not going to help. any other suggestions? Commented Apr 1, 2021 at 7:19
  • @guatidibba check Edit
    – Yash
    Commented Apr 1, 2021 at 7:23
  • the above solution of yours works well, but when the text = ' My uncle is admitted in the hospital.\n He has been suffering from 10 months.\n The address of the hospital is \nApollo Health City Campus, Jubilee Hills, Hyderabad - 500 033. India. Tel 2 +91-4023607777 Fax: +91-40-23608050\nE-mail : a;7uI|ohyd@apo{lchospita|s.I:om Website : www.apo|Iohospita|s.narn. www.apol|ohealthdty.oom Emeigency: 1066\n\n ' the output is not obtained when text is updated. Commented Apr 2, 2021 at 6:36
  • This is because i have added a check where exactly 6 numbers should be matched in a sentence split by \n. replace ==6 with >=6 and it will work.
    – Yash
    Commented Apr 2, 2021 at 7:55

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.