Use of Python regex on multiple lines of an address

Viewed 36

I have an address with the following structure:

Address: Name of business

Address line #1,

Address line #2 Pincode

I created the following regex statement : re.findall(r'Address : (.*)', text, re.MULTILINE)

I presume that this would find all the lines from Address: to the 6 digit pincode.

But it only seems to be pulling the first line. What am I doing wrong?

1 Answers

This should work fine, unless you have an issue with spaces around the colon (which are inconsistent in your post).

import re

text = """
Address: Name of business
Address: line #1,
Address: line #2 Pincode
"""

for match in re.findall(r'Address: (.*)', text, re.MULTILINE):
    print(match)

Output:

Name of business
line #1,
line #2 Pincode
Related