首页 > 解决方案 > 使用正则表达式 Dart/Flutter 提取数据

问题描述

我想从以下内容中提取数据

Periode Aantal uur Sv-loon
01-06-2019 t/m 30-06-2019 35 € 800,00
01-05-2019 t/m 31-05-2019 35 € 1.056,00
01-04-2019 t/m 30-04-2019 35 € 800,00
01-03-2019 t/m 31-03-2019 35 € 800,00
01-02-2019 t/m 28-02-2019 35 € 800,00
Datum: 06 augustus 2019

预期的输出是:

01-06-2019 t/m 30-06-2019 35 € 800,00
01-05-2019 t/m 31-05-2019 35 € 1.056,00
01-04-2019 t/m 30-04-2019 35 € 800,00
01-03-2019 t/m 31-03-2019 35 € 800,00
01-02-2019 t/m 28-02-2019 35 € 800,00

检查到目前为止我尝试过的 示例

标签: regexflutterdart

解决方案


You may use

Sv-loon\s*([\s\S]*?)\s*Datum:

See the regex demo. Details:

  • Sv-loon - a literal string
  • \s* - 0 or more whitespaces
  • ([\s\S]*?) - Group 1: any 0 or more chars as few as possible
  • \s* - 0 or more whitespaces
  • Datum: - a literal string

See Dart demo:

String txt = "Periode Aantal uur Sv-loon\n01-06-2019 t/m 30-06-2019 35 € 800,00\n01-05-2019 t/m 31-05-2019 35 € 1.056,00\n01-04-2019 t/m 30-04-2019 35 € 800,00\n01-03-2019 t/m 31-03-2019 35 € 800,00\n01-02-2019 t/m 28-02-2019 35 € 800,00\nDatum: 06 augustus 2019";
 RegExp rx = RegExp(r'Sv-loon\s*([\s\S]*?)\s*Datum:');
 Match match = rx.firstMatch(txt);
 if (match != null) {
     print(match.group(1));
 }

Output

01-06-2019 t/m 30-06-2019 35 € 800,00
01-05-2019 t/m 31-05-2019 35 € 1.056,00
01-04-2019 t/m 30-04-2019 35 € 800,00
01-03-2019 t/m 31-03-2019 35 € 800,00
01-02-2019 t/m 28-02-2019 35 € 800,00

推荐阅读