Skip to content

Commit

Permalink
Regex: support more grouping functions
Browse files Browse the repository at this point in the history
In case of multiple matches we only had an option to return a sum of
numeric values. Add more functions.

Signed-off-by: Rafał Miłecki <[email protected]>
  • Loading branch information
Rafał Miłecki authored and bosd committed Feb 25, 2023
1 parent b1cdfb7 commit f21e55a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 3 additions & 2 deletions TUTORIAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ Optional properties:

- `type` (if present must be one of: `int`, `float`, `date`) -results
in parsing every matched value to a specified type
- `group` (if present must be `sum`) - results in grouping all matched
values using specified method
- `group` (if present must be one of: `sum`, `min`, `max`, `first`,
`last`) - specifies grouping function (defines what value to return in
case of multiple matches)

Example for `regex`:

Expand Down
8 changes: 8 additions & 0 deletions src/invoice2data/extract/parsers/regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ def parse(template, field, settings, content, legacy=False):
if "group" in settings:
if settings["group"] == "sum":
result = sum(result)
elif settings["group"] == "min":
result = min(result)
elif settings["group"] == "max":
result = max(result)
elif settings["group"] == "first":
result = result[0]
elif settings["group"] == "last":
result = result[-1]
else:
logger.warning("Unsupported grouping method: " + settings["group"])
return None
Expand Down

0 comments on commit f21e55a

Please sign in to comment.