regex stuff

This commit is contained in:
Christoph J. Scherr 2023-09-05 16:15:17 +02:00
parent 2e23190dfe
commit 7d1c18101a
2 changed files with 21 additions and 1 deletions

View File

@ -242,7 +242,7 @@ The text is large, read it [here](data/metasyntactic.md) and find the raw text f
</details> </details>
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. 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, It is not allowed to store the text in source code, you must load it from an outside source,
such as a file. such as a file.
@ -252,6 +252,8 @@ Examples:
| Original | is Match? | | Original | is Match? |
|----------|-----------| |----------|-----------|
| foo | yes | | foo | yes |
| foobar | yes |
| tayfoo | no |
| baz | no | | baz | no |
| qux | no | | qux | no |
| Foo | no | | Foo | no |

18
src/tasks/regex.py Normal file
View File

@ -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}.")