首页 > 解决方案 > Dart - 从列表中提取数字

问题描述

我有一个包含彩票号码 列表图像的列表

如何分隔数字和文本,然后在下面的列表中列出它们:例如:

  List<String> n = ["ABC23", "21A23", "12A312","32141A"];

谢谢!

标签: listflutterdart

解决方案


您可以通过使用 forEach 来做到这一点,如下所示:

List<String> n = ["23", "2123", "12312","32141"];
n.forEach((element) => 
    print(element)
);

要分隔数字和文本,您可以使用以下代码:

const text = '''
Lorem Ipsum is simply dummy text of the 123.456 printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an 12:30 unknown printer took a galley of type and scrambled it to make a
23.4567
type specimen book. It has 445566 survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
''';
final intRegex = RegExp(r'\s+(\d+)\s+', multiLine: true);
final doubleRegex = RegExp(r'\s+(\d+\.\d+)\s+', multiLine: true);
final timeRegex = RegExp(r'\s+(\d{1,2}:\d{2})\s+', multiLine: true);
void main() {
  print(intRegex.allMatches(text).map((m) => m.group(0)));
  print(doubleRegex.allMatches(text).map((m) => m.group(0)));
  print(timeRegex.allMatches(text).map((m) => m.group(0)));
}

请参阅此链接了解更多信息


推荐阅读