0

I am trying to pull out values for lat and lng for the following:

coordinates = 
[<div class="store-map">\n<div id="map" style="width: 100%; height: 400px;"></div>\n<script>\r\n                function initMap() {\r\n                    var myLatLng = {\r\n                        lat: 42.050994,\r\n                        lng: -88.077711                    };\r\n\r\n     

However, when I apply this regex -

found = re.search('lat:(.*),', coordinates,).group(1)  

Everything after "lat:" is returned.
However, the desired result is just the number, that stops as soon as it reaches the comma. This is odd to me, because even rubular shows that code should work. Any ideas on what I could be doing wrong here?

P.S. I have spent a bit of time, and looked at all related solutions on stackoverflow, however - no dice.

3
  • Can you share for what example of latitude it's failing? Commented Oct 10, 2017 at 8:21
  • Just use r'lat:(\d[\d.]*)' Commented Oct 10, 2017 at 8:22
  • Hi ShreyasG, example is defined as coordinates = at the beginning of the post. But it looks like Roman has a working solution already! Commented Oct 10, 2017 at 8:52

3 Answers 3

3

The right way with re.findall function:

import re

coordinates = '[<div class="store-map">\n<div id="map" style="width: 100%; height: 400px;"></div>\n<script>\r\n                function initMap() {\r\n                    var myLatLng = {\r\n                        lat: 42.050994,\r\n                        lng: -88.077711                    };\r\n\r\n '
result = re.findall(r'\b(?:lat|lng): -?\d+\.\d+', coordinates)

print(result)

The output:

['lat: 42.050994', 'lng: -88.077711']
Sign up to request clarification or add additional context in comments.

2 Comments

Note there is no need in the outer capturing group. If there is no capturing group in a pattern, re.findall return a list with match values only.
@WiktorStribiżew, sure, that was unintentionally, fixed
1

Use the following to extract the two values:

import re

text = """[<div class="store-map">\n<div id="map" style="width: 100%; height: 400px;"></div>\n<script>\r\n                function initMap() {\r\n                    var myLatLng = {\r\n                        lat: 42.050994,\r\n                        lng: -88.077711                    };\r\n\r\n     """

lat, lng = map(float, re.findall(r'(?:lat|lng):\s+([0-9.-]*?)[, ]', text))
print lat, lng

Giving you two floats as:

42.050994 -88.077711

Comments

0

This is because .* is greedy meaning it would match everything up to the last comma. Change it to .*?:

lat:(.*?),
       ^
   add this

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.