ARTICLE AD BOX
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 5998sum is third number from right.
How to extract services rows without Subtotal rows?
Result should be
Service1 1 Service2 2 Service3 4Regex 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 428.3k67 gold badges218 silver badges397 bronze badges
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]}"); } } } }41.5k18 gold badges58 silver badges88 bronze badges
Explore related questions
See similar questions with these tags.
