0

Searching in the code of a Google Maps result. My regex to get an address:

Regex re_addressen = new Regex("\"\\w*\\s[0-9]+,\\s[0-9][0-9][0-9][0-9]\\s\\w\\w\\s\\w,*");

(Searching for all the addressess in every google maps result)

But then I only get addresses that are one word. Some addresses have 2 words. I want those aswell seen in my listbox.

My regex idea here is:

Regex re_addressen = 
    new Regex("\"\\w*\\s\\w*[0-9]+,\\s[0-9][0-9][0-9][0-9]\\s\\w\\w\\s\\w,*");

But still, only addresses that are one word long.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Onno
  • 11
  • 4
  • 1
    I hope the result you try to parse with regex is not json. – Eser Aug 30 '15 at 11:10
  • I never fully got what json is. But if it means that I get (locate) certain information from a (j) script and filter this into a (string) listbox. Then yes. – Onno Aug 30 '15 at 11:27

1 Answers1

2

It seems like you are trying to parse an html with regex. Don't do it.

Instead find an API from google, that returns an xml or json and use it. See this url, for example.

Now all you have to do is downloading this url and parsing the json result with an appropriate json parser. I'll use Json.Net for this

using (var client = new HttpClient())
{
    var query = "big bang";
    var urlx = "http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address=" + WebUtility.UrlEncode(query);

    //An optional language. default is en-US
    //client.DefaultRequestHeaders.AcceptLanguage.Add(new StringWithQualityHeaderValue("en-US"));
    //client.DefaultRequestHeaders.AcceptLanguage.Add(new StringWithQualityHeaderValue("ru-RU"));
    //client.DefaultRequestHeaders.AcceptLanguage.Add(new StringWithQualityHeaderValue("ar-EG"));

    var json = await client.GetStringAsync(urlx);
    dynamic obj = JsonConvert.DeserializeObject(json);

    foreach (var res in obj.results)
    {
        string address = res.formatted_address;
        decimal lat = res.geometry.location.lat;
        decimal lng = res.geometry.location.lng;

        Console.WriteLine(string.Format("{0},{1}=>{2}", lat, lng, address));
    }
}
Community
  • 1
  • 1
EZI
  • 15,209
  • 2
  • 27
  • 33