Search code examples
regexdartwaze

Checking if a string is valid Waze Live Map link


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.


Solution

  • 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