Last active
April 21, 2023 19:32
-
-
Save ggicci/a6025c1e70bfcb3a2a39e88458f74845 to your computer and use it in GitHub Desktop.
Find acrossing multilines content blocks in pattern from a text file and process each block
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import re | |
# 1. define your block by a regular expression | |
RE_BLOCK = re.compile( | |
r"MariaDB \[test\]> desc (?P<name>\w+?);\n(?P<definition>.+?)\n\d+? rows in set.+?", | |
re.DOTALL | re.MULTILINE, | |
) | |
content = open("./schema.txt", "rt").read() | |
# 2. implement your logic of processing each matched block content | |
def process_block(block: re.Match): | |
print(block.group(0)) | |
print(block.group("name")) | |
print(block.group("definition")) | |
# driver code | |
for block in RE_BLOCK.finditer(content): | |
process_block(block) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment