Jump to content

StringRegExp Help


 Share

Recommended Posts

I take this value, in lng: 16.2478996
This is the string:
"bounds" : {
               "northeast" : {
                  "lat" : 39.3137195,
                  "lng" : 16.2478996
               },
               "southwest" : {
                  "lat" : 39.3137169,
                  "lng" : 16.2478815
               }
            },
            "location" : {
               "lat" : 39.3137169,
               "lng" : 16.2478996
            },
            "location_type" : "RANGE_INTERPOLATED",
            "viewport" : {
               "northeast" : {
                  "lat" : 39.3150671802915,
                  "lng" : 16.2492395302915
               },
               "southwest" : {
                  "lat" : 39.3123692197085,
                  "lng" : 16.24654156970849
               }
            }

The value is in here (in lng):

            "location" : {
               "lat" : 39.3137169,
               "lng" : 16.2478996

Let's say "lat" and "lon" can take on any value, just that it is a number with a dot and then another number. Example:

1.31

21.5790

33.12

and more...

Link to comment
Share on other sites

#include <Array.au3>

_Restituisci_latitudine("Via Lazio, 36, Cosenza")

Func _Restituisci_latitudine($Indirizzo)
   $oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
   $oHTTP.Open("GET", "http://maps.google.com/maps/api/geocode/json?address=" & $Indirizzo & "&sensor=false")
   $oHTTP.Send()
   $Codice_pagina = $oHTTP.Responsetext

   ; ConsoleWrite($Codice_pagina)

   $Stringa = StringRegExp($Codice_pagina, '(?s)"location"(?:.*?)(\d+.\d+),', 3)
   For $i = 0 To UBound($Stringa) - 1
      Return $Stringa[$i]
   Next
EndFunc

Thank you very much, but I tried that and it does not work.

jdelaney, it seems excessive use external UDF.

Link to comment
Share on other sites

Thanks :D but even whit

(?s)"location".*?lng.*?([\d.]+)

it doesn't work.

#include <Array.au3>

_Restituisci_latitudine("Via Lazio, 36, Cosenza")

Func _Restituisci_latitudine($Indirizzo)
   $oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
   $oHTTP.Open("GET", "http://maps.google.com/maps/api/geocode/json?address=" & $Indirizzo & "&sensor=false")
   $oHTTP.Send()
   $Codice_pagina = $oHTTP.Responsetext

   ; ConsoleWrite($Codice_pagina)

   $Stringa = StringRegExp($Codice_pagina, '(?s)"location".*?lng.*?([\d.]+)', 3)
   For $i = 0 To UBound($Stringa) - 1
      Return $Stringa[$i]
   Next
EndFunc

I just need a value so do not know whether it is right to use For... :3

Link to comment
Share on other sites

It does work, and btw you can only make one return.

$s = _Restituisci_latitudine("Via Lazio, 36, Cosenza")

ConsoleWrite("lng : " & $s)

Func _Restituisci_latitudine($Indirizzo)
   $oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
   $oHTTP.Open("GET", "http://maps.google.com/maps/api/geocode/json?address=" & $Indirizzo & "&sensor=false")
   $oHTTP.Send()
   $Codice_pagina = $oHTTP.Responsetext

   ConsoleWrite($Codice_pagina)

   $Stringa = StringRegExp($Codice_pagina, '(?s)"location"(?:.*?)(\d+.\d+),', 3)
   Return $Stringa[0]
EndFunc
Edited by FireFox
Link to comment
Share on other sites

Confirmation  :)

#include <Array.au3>

$s = _Restituisci_latitudine("Via Lazio, 36, Cosenza")
_ArrayDisplay($s)

Func _Restituisci_latitudine($Indirizzo)
   $oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
   $oHTTP.Open("GET", "http://maps.google.com/maps/api/geocode/json?address=" & $Indirizzo & "&sensor=false")
   $oHTTP.Send()
   $Codice_pagina = $oHTTP.Responsetext

   Return StringRegExp($Codice_pagina, '(?s)location".*?lat.*?([\d.]+).*?lng.*?([\d.]+)', 3)
EndFunc

You can make one return only but this can be an array  ;)

Edited by mikell
Link to comment
Share on other sites

               "lat" : 36.1805194,
               "lng" : -86.72654829999999

Before the number there could also be a hyphen. How do I resolve with:

(?s)location".*?lat.*?([\d.]+).*?lng.*?([\d.]+)

?

Thanks. :)

Link to comment
Share on other sites

In this context it's unlikely that Google would have fun with you by returning 5-6.7-523...47-5

To remove the possibility of wrong input, use this instead:

(?s)location".*?lat.*?(-?d+(?:.d+)?).*?lng.*?(-?d+(?:.d+)?)

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...