Checking if a string is valid Waze Live Map link

336 views Asked by At

I am trying to check if a string is a valid deep link from Waze Live Map. A Waze Live Map deep link looks something like this:

https://www.waze.com/ul?place=ChIJj61dQgK6j4AR4GeTYWZsKWw&ll=37.42199990%2C-122.08405750&navigate=yes

Given a string, I want to know if it is a valid Waze live map link. Most important, I want to know if it has the longitude and latitude at least.

I have tried the following:

String str = 'https://www.waze.com/ul?place=ChIJj61dQgK6j4AR4GeTYWZsKWw&ll=37.42199990%2C-122.08405750&navigate=yes'; 
var wazeUrlPattern = r"https:\/\/www.waze.com\/ul\?ll\=(.)*([1-9]+\.[1-9]+)%2C([1-9]+\.[1-9]+)(.)?";
bool valid = new RegExp(wazeUrlPattern, caseSensitive:false).hasMatch(str);
if(!valid) {
    print("The url is not valid waze live map link!");
    return;
  }
// Do something with the longitude and latitude and maybe other parameters

This is not working for me and I would like some help in figuring out the problem.

5

There are 5 answers

0
Ryszard Czech On BEST ANSWER

Use

https://www\.waze\.com/ul\?.*?&ll=(-?\d+\.\d+)%2C(-?\d+\.\d+)

See proof

Escape dots to match literal dots and you have ?ll while there is ?place and &ll= later in the URL. Also, longitude and latitude may have optional - in front.

EXPLANATION

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  https://www              'https://www'
--------------------------------------------------------------------------------
  \.                       '.'
--------------------------------------------------------------------------------
  waze                     'waze'
--------------------------------------------------------------------------------
  \.                       '.'
--------------------------------------------------------------------------------
  com/ul                   'com/ul'
--------------------------------------------------------------------------------
  \?                       '?'
--------------------------------------------------------------------------------
  .*?                      any character except \n (0 or more times
                           (matching the least amount possible))
--------------------------------------------------------------------------------
  &ll=                     '&ll='
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    -?                       '-' (optional (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
    \.                       '.'
--------------------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  %2C                      '%2C'
--------------------------------------------------------------------------------
  (                        group and capture to \2:
--------------------------------------------------------------------------------
    -?                       '-' (optional (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
    \.                       '.'
--------------------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
  )                        end of \2
0
jdaz On

You could use this:

var wazeUrlPattern = r"https://www\.waze\.com/ul\?.*ll=[\d.-]+%2C[\d.-]+";

Demo

0
mjrezaee On

You may use following regex:

https:\/\/www\.waze\.com\/ul\?.*?ll\=(\d+\.\d+)%2C-(\d+\.\d+)

Regex Demo

Details

https:\/\/www\.waze\.com\/ul\?.*?\&: matches url til ll argument

(\d+\.\d+): combination of digits and one dot character

%2C-: characters between coordination

0
marianc On

Try this regular expression:

https:\/\/www\.waze\.com\/ul\?place=([^&]*)&ll=-?([1-9]\d*\.\d+)%2C-?([1-9]\d*\.\d+).*

For convenience you will have place on $1 (Group 1) and the remaining groups will provide the longitude and latitude.

Please check the demo here.

0
lrn On

Consider not using a RegExp. If you first parse the input as a URI, you get normalization an can still query the individual parts:

bool isWazeDeepLink(String url) {
  var parsedUrl = Uri.parse(url);
  if (parsedUrl.isScheme("http") && 
      parsedUrl.host == "www.waze.com" &&
      parsedUrl.path == "/ul" &&
      parsedUrl.queryParameters["ll"] != null) {
    // Or check format of the `ll` parameter
    return true;
  }
  return false;
}

You can also use a RegExp, but you should consider that the scheme and host fields are case insensitive, and so is the %2c escape, but the path isn't, so you'd want something like:

 var re = RegExp(r"^[hH][tT][tT][pP]://[wW]{3}\.[wW][aA][zZ][eE]\.[cC][oO][mM]"
         r"/ul\?.*(?<=[?&])ll=(?:-?\d+(?:\.\d+))%2[C](?:-?\d+(?:\.\d+))(?:[&#]|$)");

This will match http://www.waze.com/ in any case, then ul as lower case, and then any following string containing a complete URL parameter (prefixed by ? og &, followed by &, # or the end), which has the format ll= followed by a number, %2c or %2C and another number.

(Or you can start by parsing the input as a URI, then doing a toString to get a normalized URI, then the other regexps suggested here will work as well).