Reputation: 1596
I have a string containing 3 or 4 double numbers. what's the best way to extract them in an array of numbers?
Upvotes: 3
Views: 1788
Reputation: 71903
First you have to find the numerals. You can use a RegExp
pattern for that, say:
var doubleRE = RegExp(r"-?(?:\d*\.)?\d+(?:[eE][+-]?\d+)?");
Then you parse the resulting strings with double.parse
. Something like:
var numbers = doubleRE.allMatches(input).map((m) => double.parse(m[0])).toList();
Upvotes: 6