Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extract a substring from a string in Python – regex

Let’s say I have a long string with address data in format:

string = ... 'Something 3; Place: Something; City: NewYork; Street: Broadway; House: 7; Lat g: 33,105438; Long g: 12,749558; Object: Something;'...

I want to extract substring from that string starting from Place: to Long g: 12,749558. usinge regex findall().
Can anyone help me? Thanks!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I want to get from my string:

Place: Something; City: NewYork; Street: Broadway; House: 7; Lat g: 33,105438; Long g: 12,749558;

>Solution :

You can use the following regex pattern: r'Place:.*?Long g: \d+,\d+;'.

Code example:

import re

string = '... Something 3;  Place: Something; City: NewYork; Street: Broadway; House: 7; Lat g:  33,105438; Long g: 12,749558; Object: Something;...'
pattern = r'Place:.*?Long g: \d+,\d+;'
result = re.findall(pattern, string) # "Place: Something; City: NewYork; Street: Broadway; House: 7; Lat g:  33,105438; Long g: 12,749558;"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading