regex task solution

This commit is contained in:
Christoph J. Scherr 2023-09-05 16:19:21 +02:00
parent 7d1c18101a
commit 06194f0903
2 changed files with 14 additions and 3 deletions

View File

@ -267,12 +267,23 @@ Use [regex101.com](https://regex101.com) if you are not already a REGEX expert.
<details> <details>
<summary>Hints</summary> <summary>Hints</summary>
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.
</details> </details>
<details> <details>
<summary>Solution</summary> <summary>Solution</summary>
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)
</details> </details>

View File

@ -13,6 +13,6 @@ regex = r"\b[a-z][AEIOUaeiou]([a-w]|[A-W])"
matches = re.finditer(regex, text, re.MULTILINE) matches = re.finditer(regex, text, re.MULTILINE)
counter = 0 counter = 0
for i, match in enumerate(matches, start=1): for i, match in enumerate(matches, start=1):
#print(f"{i:03} | \"{match.string}\"") print(f"{i:03} | \"{match}\"")
counter += 1 counter += 1
print(f"found {counter}.") print(f"found {counter}.")