Regex Expression Non Capturing Group

时间:2019-04-08 13:42:34

标签: regex

I am trying to read out an amount on a invoice.
The line in question (there are a few other lines that include € but not the word 'Total') looks like this:
Total € 31.73
I want my full match to be only the digits (including the point) not the € symbol right in front of it.

What I tried is a non-capturing group for € but my full match still includes it.

This is the expression i tried:

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

1 个答案:

答案 0 :(得分:1)

Try the following regex pattern:

Total\s+(?:€\s)?(\d{1,3}(?:,\d{3})*(?:\.\d+)?)

The total amount would be present in the first capture group.

Demo

Here is a brief explanation of the regex:

Total\s+         match "Total" followed by one or more spaces
(?:€\s)?         match an optional Euro sign, followed by a space
(                capture
    \d{1,3}      match one to three digits
    (?:,\d{3})*  followed by zero or more thousands groups
    (?:\.\d+)?   followed by an optional decimal component
)                stop capture

Edit:

If you want a pattern whose entire match is only the numeric portion, then try using this:

(?:(?<=Total )|(?<=Total € ))\d{1,3}(?:,\d{3})*(?:\.\d+)?

Demo

This asserts that either Total or Total € comes first, followed by the number. Note that it does not actually match the prefix, but only matches the number.