【问题标题】:how to isolate decimal in String expression in Dart flutter如何在 Dart Flutter 中的字符串表达式中隔离小数
【发布时间】:2018-11-17 08:37:45
【问题描述】:

我有一个这样的字符串:

Date:X/X/XX
Time:XX:XX:XX
Speed:Xkm/h
Altitude:XX.Xm
Bat:XX%
maps:google.com/maps?q=YY.YYYYYY,-Z,ZZZZZZ

目前这个正则表达式没问题:https://regex101.com/r/5JVdgR/1

但我不知道如何显示 Y(纬度变量)和 Z(经度变量)?

这是我搜索的内容的一个想法:

 var input = "Date:X/X/XX"              //Here is my input variable
 "Time:XX:XX:XX"
 "Speed:Xkm/h"
 "Altitude:XX.Xm"
 "Bat:XX%"
 "maps:google.com/maps?q=14.215465,-1.256584";

void test() {          //On push, I want to extract group1 (lat ) and group 2 (long) into two variable
setState(() {

  RegExp regExp = new RegExp(            //Here is the regex fonction to extract long, lat
    r"maps:google\.com\/maps\?q=(-?[0-9]+.[0-9]+),(-?[0-9]+.[0-9]+)",
   );
  }
 );
}

@override
Widget build(BuildContext context) {

return new Scaffold(
 appBar: new AppBar(
 ),
 body: new Center(
 child: new Column(
  mainAxisAlignment: MainAxisAlignment.center,
  children: <Widget>[
    new Text(

      $group1,$group2         //I want to display each variables extracted by regex

    ),
  ],
 ),
),
  floatingActionButton: new FloatingActionButton(
  onPressed: test,
  tooltip: 'test',
  child: new Icon(Icons.add),
    ),
   );
  }
}

【问题讨论】:

  • q=YY.YYYYYY,-Z,ZZZZZZ 这里,Z侧的“,”对吗?不是像 Y 中的点吗?

标签: regex dart flutter


【解决方案1】:

尝试正则表达式:maps:google\.com\/maps\?q=(-?\d+(?:(?:\.|,)\d+)?),(-?\d+(?:(?:\.|,)\d+)?) 并获取 Group1 和 Group2 值

Demo

link 有一个在 Dart 中使用正则表达式的好例子

【讨论】:

    【解决方案2】:

    假设 -Z,ZZZZZZ 将是 -Z.ZZZZZZ 用点而不是逗号,您可以在 2 个捕获组 (-?[0-9]+.[0-9]+) 中捕获您的值。

    Y 的值将在第 1 组中,Z 的值将在第 2 组中。

    maps:google\.com\/maps\?q=(-?[0-9]+.[0-9]+),(-?[0-9]+.[0-9]+)

    【讨论】:

      猜你喜欢
      • 2021-08-10
      • 2023-01-06
      • 2020-08-13
      • 2022-12-19
      • 2022-11-25
      • 2019-08-21
      • 2015-06-13
      • 2023-03-26
      • 1970-01-01
      相关资源
      最近更新 更多