Invoice contains services and Subtotal rows:

Service1 1058 187822_MB 1 000 000 Service2 48 02:42:39 2 000 000 Subtotal 3 302 5998 Service3 8 4 000 000 Subtotal 4 302 5998

sum is third number from right.

How to extract services rows without Subtotal rows?

Result should be

Service1 1 Service2 2 Service3 4

Regex https://regex101.com/r/ZKUMtQ/1

(?<nimetus>(?!(Subtotal))\S+)[^\n]*? (?<sum>\d+) \d+ \d+

returns also subtotal rows without first letter S

Service1 1 Service2 2 ubtotal 3 Service3 4 ubtotal 4

Andrus's user avatar

2

You don't need regex here. In fact it would make code in this case not much readable.

I would recommend more explicit approach:

Split line by space Check if it has more than 3 items. Access third item from the end.

C# code:

using System; public class Program { public static void Main() { string text = @"Service1 1058 187822_MB 1 000 000 Service2 48 02:42:39 2 000 000 Subtotal 3 302 5998 Service3 8 4 000 000 Subtotal 4 302 5998"; foreach (var line in text.Split(Environment.NewLine)) { var parts = line.Split(' ', StringSplitOptions.RemoveEmptyEntries); if (parts.Length >= 3) { Console.WriteLine($"{parts[0]} {parts[^3]}"); } } } }

C# fiddle

Michał Turczyn's user avatar

// rows is IEnumerable<string>

var services = rows.Where(row => row.StartsWith("Service"));

Frank_J's user avatar

1 Comment

You missed half the requirements of the question here.

2026-01-19T06:54:33.517Z+00:00

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.