Extract data using regex Dart/Flutter

Viewed 188

I want to extract data from the following

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

The expected output is :

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

Check what I tried so far example

2 Answers

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
RegExp re = new RegExp("((?<=Sv-loon)([\\S\\s]*?)(?=Datum:))");

Where ?= is a Positive Lookahead.

Related