【问题标题】:Checking if a string is valid Waze Live Map link检查字符串是否有效 Waze Live Map 链接
【发布时间】:2020-11-22 08:07:55
【问题描述】:

我正在尝试检查字符串是否是来自 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

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

【问题讨论】:

  • 我会尽力提供帮助,但我不知道什么是深度位智的格式。也许您可以列出一些特定的允许字符等等。或者,您似乎知道正则表达式,您遇到了什么具体的正则表达式问题?
  • 另外,我不建议创建正则表达式对象并在堆栈上调用它。
  • 请注意,根据developers.google.com/waze/deeplinks,始终具有ll 参数的位智深度链接(例如,它们可能具有导航收藏夹等的链接)

标签: regex dart waze


【解决方案1】:

考虑使用正则表达式。如果您首先将输入解析为 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;
}

您也可以使用 RegExp,但您应该考虑到 schemehost 字段不区分大小写,%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 &amp;,后接&amp;,@987654329 @ 或结尾),其格式为 ll= 后跟一个数字、%2c%2C 和另一个数字。

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

【讨论】:

    【解决方案2】:

    试试这个正则表达式:

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

    为方便起见,您将在 1 美元(第 1 组)上占有一席之地,其余组将提供经度和纬度。

    请查看demo here

    【讨论】:

      【解决方案3】:

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

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

      Regex Demo

      详情

      https:\/\/www\.waze\.com\/ul\?.*?\&amp;:匹配 url 直到 ll 参数

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

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

      【讨论】:

        【解决方案4】:

        使用

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

        proof

        转义点以匹配文字点,您有?ll,而在URL 后面有?place&amp;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
        

        【讨论】:

          【解决方案5】:

          你可以用这个:

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

          Demo

          【讨论】:

            猜你喜欢
            • 2019-08-21
            • 2010-09-30
            • 2019-05-05
            • 2019-10-04
            • 1970-01-01
            • 2013-02-13
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多