1

我正在尝试检查字符串是否是来自 Waze Live Map 的有效深层链接。Waze Live Map深层链接如下所示:

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

给定一个字符串,我想知道它是否是有效的 Waze 实时地图链接。最重要的是,我想知道它是否至少有经度和纬度。

我尝试了以下方法:

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

这对我不起作用,我需要一些帮助来解决问题。

4

5 回答 5

2

考虑使用 RegExp。如果您首先将输入解析为 URI,您将获得规范化,并且仍然可以查询各个部分:

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;
}

您也可以使用正则表达式,但您应该考虑方案主机字段不区分大小写,%2c转义也是如此,但路径不是,所以您需要类似:

 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+))(?:[&#]|$)");

这将http://www.waze.com/在任何情况下匹配,然后ul是小写,然后是包含完整 URL 参数(以?og为前缀,&后跟或结尾)的任何后续字符串,其格式为后跟一个数字或另一个数字。&#ll=%2c%2C

(或者您可以先将输入解析为 URI,然后执行 atoString以获取规范化的URI,然后此处建议的其他正则表达式也可以使用)。

于 2020-08-02T07:17:12.007 回答
1

你可以使用这个:

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

演示

于 2020-08-01T19:54:13.203 回答
1

采用

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

证明

转义点以匹配文字点,并且您在 URL?ll?place和稍后都有。&ll=此外,经度和纬度可以-在前面有可选的。

解释

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
于 2020-08-01T19:57:03.843 回答
1

您可以使用以下正则表达式:

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

正则表达式演示

细节

https:\/\/www\.waze\.com\/ul\?.*?\&: 匹配 url tilll参数

(\d+\.\d+): 数字和一个点字符的组合

%2C-: 字符之间的协调

于 2020-08-01T19:57:46.077 回答
1

试试这个正则表达式:

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

为方便起见,您将有 1 美元(第 1 组)的位置,其余组将提供经度和纬度。

请在此处查看演示

于 2020-08-01T20:19:30.773 回答