Skip to content

Instantly share code, notes, and snippets.

@ggicci
Last active April 21, 2023 19:32
Show Gist options
  • Save ggicci/a6025c1e70bfcb3a2a39e88458f74845 to your computer and use it in GitHub Desktop.
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
#!/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