From 06194f09038709c090d8b6f65cb7ee2662563d3e Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Tue, 5 Sep 2023 16:19:21 +0200 Subject: [PATCH] regex task solution --- Tasks.md | 15 +++++++++++++-- src/tasks/regex.py | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Tasks.md b/Tasks.md index e833f33..9dd9c6b 100644 --- a/Tasks.md +++ b/Tasks.md @@ -267,12 +267,23 @@ Use [regex101.com](https://regex101.com) if you are not already a REGEX expert.
Hints -TODO +- use `open()` to open your file for reading. +- use the `read()` method to read the file out. +- Use the `re` library +- Use `\b` to match a word boundary +- Use ranges `[5-9]` +- You can set a higher precedence by putting things in braces `(ABC)`. +- You can connect two expressions with `A|B` to use either `A` or `B` +- Use global mode.
Solution -TODO +There should be $374$ matches. + +A regex that fullfills the requirements is `\b[a-z][AEIOUaeiou]([a-w]|[A-W])`. + +[Code Example](src/tasks/regex.py)
diff --git a/src/tasks/regex.py b/src/tasks/regex.py index 2f44892..9d50e2c 100644 --- a/src/tasks/regex.py +++ b/src/tasks/regex.py @@ -13,6 +13,6 @@ regex = r"\b[a-z][AEIOUaeiou]([a-w]|[A-W])" matches = re.finditer(regex, text, re.MULTILINE) counter = 0 for i, match in enumerate(matches, start=1): - #print(f"{i:03} | \"{match.string}\"") + print(f"{i:03} | \"{match}\"") counter += 1 print(f"found {counter}.")