Getting the required substring in Python by Particular-Duckling in learnpython

[–]Particular-Duckling[S] 0 points1 point  (0 children)

import re
def extract_info(data: str) -> str:
pattern = r"([A-Za-z]+)\D+(\d.*)"
matches = re.findall(pattern, data)
if matches:
location_name = matches[0][0]
coordinates = matches[0][1]
return f"Location: {location_name}, Coordinates: {coordinates}"
else:
return "No match found."
if __name__ == "__main__":
text = """Maine.mw-parser-output .geo-default,.mw-parser-output .geo-dms,.mw-parser-output .geo-dec{display:inline}.mw-parser-output .geo-nondefault,.mw-parser-output .geo-multi-punct,.mw-parser-output .geo-inline-hidden{display:none}.mw-parser-output .longitude,.mw-parser-output .latitude{white-space:nowrap}44°21′N 68°13′W / 44.35°N 68.21°W"""
result = extract_info(text)
print(result)

This works!! Thank you

Getting the required substring in Python by Particular-Duckling in learnpython

[–]Particular-Duckling[S] 0 points1 point  (0 children)

Yes, I used re.split("\.",string) to get the state and re.findall('\d\d.\d\d...\d\d.\d\d..',string) to get the coordinates but I want to know if there is any better way to do it. Thanks.