From 4f9e2ab48d5dc2a1ba4c2ccb8de987db900d2ed8 Mon Sep 17 00:00:00 2001 From: "Mr. Pine" Date: Mon, 21 Oct 2024 15:21:33 +0200 Subject: [PATCH] Support named capture groups in regex transformers (#94) --- CHANGELOG.md | 1 + CONFIG.md | 3 ++- PFERD/transformer.py | 4 ++++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b93bd33..e56b011 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ ambiguous situations. - Generic `ilias-web` crawler and `ilias-web` CLI command - Support for the course overview page. Using this URL as a target might cause duplication warnings, as subgroups are listed separately. +- Support for named capture groups in regex transforms ### Fixed - Normalization of meeting names in cards diff --git a/CONFIG.md b/CONFIG.md index 9a6eb4a..a52506d 100644 --- a/CONFIG.md +++ b/CONFIG.md @@ -394,7 +394,8 @@ matches `SOURCE`, the output path is created using `TARGET` as template. be referred to as `{g}` (e.g. `{g3}`). `{g0}` refers to the original path. If capturing group *n*'s contents are a valid integer, the integer value is available as `{i}` (e.g. `{i3}`). If capturing group *n*'s contents are a -valid float, the float value is available as `{f}` (e.g. `{f3}`). If a +valid float, the float value is available as `{f}` (e.g. `{f3}`). Named capture +groups (e.g. `(?P)`) are available by their name (e.g. `{name}`). If a capturing group is not present (e.g. when matching the string `cd` with the regex `(ab)?cd`), the corresponding variables are not defined. diff --git a/PFERD/transformer.py b/PFERD/transformer.py index 1a56e27..a48c827 100644 --- a/PFERD/transformer.py +++ b/PFERD/transformer.py @@ -110,6 +110,10 @@ class ExactReTf(Transformation): except ValueError: pass + named_groups: Dict[str, str] = match.groupdict() + for name, capture in named_groups.items(): + locals_dir[name] = capture + result = eval(f"f{right!r}", {}, locals_dir) return Transformed(PurePath(result))