this is the same question as here:
How can I convert this string to list of lists?
but for Dart rather than python. My aim (as in the other question) is to take a string like:
String stringRep = '[[123],[122],[411]]';
And convert it to a List of lists. I can see I would be able to achieve this using one of the methods recommended in answer referenced above, namely:
str = "[[0,0,0],[0,0,1],[1,1,0]]"
strs = str.replace('[','').split('],')
lists = [map(int, s.replace(']','').split(',')) for s in strs]
But wondering if there is a better method in Dart but struggling to fnd any online?
You can use the JSON decoder
import 'dart:convert';
...
var lists = json.decode('[[123],[122],[411]]');
DartPad example
update
final regExp = new RegExp(r'(?:\[)?(\[[^\]]*?\](?:,?))(?:\])?');
final input = '[[sometext],[122],[411]]';
final result = regExp.allMatches(input).map((m) => m.group(1))
.map((String item) => item.replaceAll(new RegExp(r'[\[\],]'), ''))
.map((m) => [m])
.toList();
print(result);
DartPad example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With