regex task solution
This commit is contained in:
parent
7d1c18101a
commit
06194f0903
15
Tasks.md
15
Tasks.md
|
@ -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>
|
||||||
|
|
|
@ -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}.")
|
||||||
|
|
Loading…
Reference in New Issue