Search code examples
pythonpython-3.xregexpython-2.7regex-lookarounds

extract strings till ' \n ' character using lookbehnd in regex in python. And it can have alphanumeric characters, special characters and space also


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


Solution

  • 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.