From 7d1c18101ace5a1993c75a2f60c53e6d1caca047 Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Tue, 5 Sep 2023 16:15:17 +0200 Subject: [PATCH] regex stuff --- Tasks.md | 4 +++- src/tasks/regex.py | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 src/tasks/regex.py diff --git a/Tasks.md b/Tasks.md index 6085c39..e833f33 100644 --- a/Tasks.md +++ b/Tasks.md @@ -242,7 +242,7 @@ The text is large, read it [here](data/metasyntactic.md) and find the raw text f -1. Use a regular expression (regex) to find all instances of a lowercase character with a +1. Use a regular expression (regex) to find all words that start with a lowercase character with a following vowel character, in which no 'x', z' or 'y' follows the vowel in the given Text. It is not allowed to store the text in source code, you must load it from an outside source, such as a file. @@ -252,6 +252,8 @@ Examples: | Original | is Match? | |----------|-----------| | foo | yes | +| foobar | yes | +| tayfoo | no | | baz | no | | qux | no | | Foo | no | diff --git a/src/tasks/regex.py b/src/tasks/regex.py new file mode 100644 index 0000000..2f44892 --- /dev/null +++ b/src/tasks/regex.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 +import re +import sys + +if len(sys.argv) != 2: + print("takes only a file path as argument") + exit(1) + +textfile = open(sys.argv[1]) +text = textfile.read() + +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}\"") + counter += 1 +print(f"found {counter}.")