6

I have a list of random dates formatted like:

String x ="Text(\"key:[2020-08-23 22:22, 2020-08-22 10:11, 2020-02-22 12:14]\"),"

I can use the \d{4}\-\d{2}\-\d{2}\s\d{2}:\d{2} regex to match all dates in x:

RegExp regExp79 = new RegExp(
    r'\d{4}\-\d{2}\-\d{2}\s\d{2}:\d{2}',
);
var match79 = regExp79.allMatches("$x");
var mylistdate = match79;

So, the matches are:

match 1 = 2020-08-22 22:22
match 2 = 2020-08-22 10:11
match 3 = 2020-02-22 12:14

I want to convert the Iterable<RegExpMatch> into a list of strings, so that the output of my list looks like:

[2020-08-22 22:22, 2020-08-22 10:11, 2020-02-22 12:14]
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Nitneuq
  • 3,866
  • 13
  • 41
  • 64
  • The question is more a question of possibilities of match an impreditive and random list that a X regex, I use regex I can find how to match my list but I don't know ( in the context of a listString) if it's possible and if YES how to add multiple match groupe in a ListString – Nitneuq Sep 03 '20 at 08:34
  • Please consider adding the code you tried to solve the problem in order to help you. – Wiktor Stribiżew Sep 03 '20 at 08:36
  • it's clear stringList.add("$groupe1+groupe2+groupe3"); // try to groupe all match in a list, but don't know to to write correctly if number of group are variable prefs.setStringList("my_Key",stringList_value); // save my list with a key – Nitneuq Sep 03 '20 at 08:37
  • I post this code but with abstract representation because I havn't the solution, I need help to found this – Nitneuq Sep 03 '20 at 08:38
  • here is the regex \d{4}\-\d{2}\-\d{2}\s\d{2}:\d{2} – Nitneuq Sep 03 '20 at 08:48
  • I can match each date from my list but don't know how to add each match in my list – Nitneuq Sep 03 '20 at 08:49
  • See [this thread](https://stackoverflow.com/questions/49757486/how-to-use-regex-in-dart), you just need to use [`regExp.allMatches()`](https://api.dart.dev/stable/2.9.1/dart-core/RegExp/allMatches.html) – Wiktor Stribiżew Sep 03 '20 at 08:53
  • I edit my post , with complet code and use of allMatches but I have this issue :" the argument type 'Iterable' can't be assigned to the parameter type 'List' – Nitneuq Sep 03 '20 at 09:05
  • How do you declare `prefs`? I guess you forgot to escape `"` inside an `x` string literal, too. – Wiktor Stribiżew Sep 03 '20 at 09:08
  • don't know if I understand but I think I declare like that SharedPreferences prefs = await SharedPreferences.getInstance(); – Nitneuq Sep 03 '20 at 09:12
  • Yes I have " in x String because I use allkeys=prefs.getKeys().map((key) { return Text(key+ ":" + prefs.get(key).toString()); }).toList(); – Nitneuq Sep 03 '20 at 09:13
  • Please post the [MCVE (minimal complete verifiable example)](http://stackoverflow.com/help/mcve). Add all code necessary to repro the problem. Or remove redundant code. Do you just want to convert `mylistdate` to a string list? – Wiktor Stribiżew Sep 03 '20 at 09:15
  • My goal is to load all key value in a string, that the user can save in email. And if he change device he can past this list to restaure au key value of previous device, I have found on stack overflow a code to do this but don't found how to remove Text("...") around each key data, so I use regex to filtre this if it a problem – Nitneuq Sep 03 '20 at 09:16
  • 1
    Is `var mylistdate = match79.map((z) => z.group(0));` all you need? – Wiktor Stribiżew Sep 03 '20 at 09:18
  • Yes I have all date match in mylistdate, but want ADD match in a Stringlist. If I have 5 date match I want to save these match in index[0] index[1] index[2] index[3] index[4] of my String list – Nitneuq Sep 03 '20 at 09:19
  • Thank you I will try, it's look what I need – Nitneuq Sep 03 '20 at 09:21

1 Answers1

10

The allMatches method returns an Iterable<RegExpMatch> value. It contains all the RegExpMatch objects that contain some details about the matches. You need to invoke the .group(0) method on each RegExpMatch object to get the string value of the match.

So, you need to .map the results:

your_regex.allMatches(x).map((z) => z.group(0)).toList()

Code:

String x ="Text(\"key:[2020-08-23 22:22, 2020-08-22 10:11, 2020-02-22 12:14]\"),";
RegExp regExp79 = new RegExp(r'\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}');
var mylistdate = regExp79.allMatches(x).map((z) => z.group(0)).toList();
print(mylistdate);

Output:

[2020-08-23 22:22, 2020-08-22 10:11, 2020-02-22 12:14]
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563