1

i want to find the last word matching my expression and get the numbers as a total sum. So here is my problem. My Document says:

Vat-Total: 123.45
Total: 345.67

So i tried to get the sum with:

(?<=Total:)([\s]*)((((\d+)[,.]{1,10})+\d{0,2})|(\d+(?!,)))

But my problem is, that it finds the first occurance of Total in the first line. I want to extract the sum from the last line. Even if i try to Escape it with \b it finds the first word and does not match the last one.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
ThoMo
  • 21
  • 3
  • 1
    Add `[\s\S]*` at the start, or use the `x(?![\s\S]*x)` like regex to match the last occurrence – Wiktor Stribiżew May 22 '20 at 10:57
  • Can't you simply add a start string ancor in your positive lookbehind? `(?<=^Total:)`? – JvdV May 22 '20 at 11:40
  • If you are using capturing groups, you could also match Total: from the start of the string and capture the value in group 1. `^Total: (\d+(?:\.\d{1,2})?)$` – The fourth bird May 22 '20 at 12:10
  • Thank you all for your help, but none of the above really helped me. The [\s\S] Attribution is more if your word also breaks in line or similar. The thing with the string ancor does not recognize the last one. It Stil recognizes the first because of the "-" before it. I tried to group it like @Thefourthbird said, but i was not lucky with that one. Maybe my code is wrong here: (^Total: (\d+(?:\.\d{1,2})?)$)([\s]*)((((\d+)[,.]{1,10})+\d{0,2})|(\d+(?!,))) – ThoMo May 22 '20 at 14:16
  • Is the example data from the question always structured like that? So Total: is located at the beginning of the line? Did you try enabling multiline? https://regex101.com/r/pwNMXk/1 What is the tool or language? Can you extend the example in the question? – The fourth bird May 22 '20 at 14:21

1 Answers1

0

Add [\s\S]* at the start, or use the x(?![\s\S]*x) like regex to match the last occurrence by Wiktor Stribizew is the solution.

I simplified the number regex to \d+(?:[,.]\d{1,2})? and here are proofs the above works:

[\s\S]*Total:\s*(\d+(?:[,.]\d{1,2})?)

See proof. Another approach:

(?<=Total:)\s*(\d+(?:[,.]\d{1,2})?)(?![\s\S]*Total:\s*\d)

See another proof

Ryszard Czech
  • 18,032
  • 4
  • 24
  • 37