I have a simple problem using Regex with python...
I am trying to grab GPS coordinates out of a line that will be formatted as such:
-73209460,44477974
Sometimes it will have the leading '-', sometimes it wont. Consequently, my regex is as follows:
'-?\d\d\d\d\d\d\d\d,\d\d\d\d\d\d\d\d'
Here's the weird part:
I have tested this query on solely the GPS coordinate string (above) and it works fine, matches it, and acts as intended. However when I try to get it to match the GPS coords when there is "noise" around it, it does not catch it... Here is one line that the GPS coords are in:
RoutingRequest,1391101299,3,-10,1,-1,1000,-73209460,44477974,-1,56862713,56862712,
Any idea why it would not be catching this? Something to do with the commas?
Thanks guys.
EDIT:
Thanks for all the responses... Here is a quick test script that I was using:
import re
def doesntWork():
gpsCoords = r'-?\d\d\d\d\d\d\d\d,\d\d\d\d\d\d\d\d'
gps = re.compile(gpsCoords)
match = gps.match("1000,-73209460,44477974,-1,56862")
if match:
return True
else:
return False
def works():
gpsCoords = r'-?\d\d\d\d\d\d\d\d,\d\d\d\d\d\d\d\d'
gps = re.compile(gpsCoords)
match = gps.match("-73209460,44477974")
if match:
return True
else:
return False
def main():
print doesntWork()
print works()
main()
Aside from the fact your regex looks a little fragile, this is likely due to using re.match
instead of re.search
.
re.match
tries to match the regex to the beginning of your string, where as re.search
tries to find a match anywhere in your string.
The difference is explained in the python documentation here: https://docs.python.org/3/library/re.html#search-vs-match
As SpoonMeiser said, you should be using re.search()
. Here's a little more sophisticated regex which captures both lat and lon in named groups`.. Assuming the last values are longitude values.. This might break but it's a nice idea.
>>> r = "RoutingRequest,1391101299,3,-10,1,-1,1000,-73209460,44477974,-1,56862713,56862712"
>>> re.search(r"(?P<lat>-?\d{8},\d{8}).*(?P<lon>\d{8},\d{8})", r).groupdict()
{'lat': '-73209460,44477974', 'lon': '56862713,56862712'}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.