如何在Dart flutter中的String表达式中分隔十进制

时间:2018-06-07 12:20:00

标签: regex dart flutter

我有一个这样的字符串:

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

2 个答案:

答案 0 :(得分:1)

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

Demo

这个link就是在Dart中使用Regex的一个很好的例子

答案 1 :(得分:0)

假设-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]+)